This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-14
Channels
- # announcements (2)
- # aws (1)
- # babashka (18)
- # babashka-sci-dev (103)
- # beginners (165)
- # calva (51)
- # cider (8)
- # circleci (1)
- # clj-kondo (22)
- # clj-on-windows (2)
- # cljdoc (1)
- # cljfx (31)
- # cljs-dev (16)
- # clojure (81)
- # clojure-europe (71)
- # clojure-nl (7)
- # clojure-uk (11)
- # clojurescript (20)
- # code-reviews (26)
- # conjure (1)
- # contributions-welcome (1)
- # core-async (15)
- # cursive (8)
- # datomic (8)
- # defnpodcast (2)
- # eastwood (24)
- # emacs (10)
- # events (1)
- # fulcro (4)
- # funcool (31)
- # graalvm (43)
- # graphql (8)
- # honeysql (9)
- # introduce-yourself (1)
- # jobs (12)
- # kaocha (3)
- # lsp (28)
- # malli (4)
- # meander (4)
- # membrane (7)
- # off-topic (64)
- # other-languages (3)
- # pedestal (1)
- # polylith (31)
- # portal (5)
- # re-frame (4)
- # reitit (1)
- # releases (5)
- # rum (2)
- # schema (2)
- # sci (34)
- # shadow-cljs (21)
- # vscode (1)
question: I’m sending code to an endpoint on the server which (sci/eval-string …)
’s and I need to return the output of the call back to the browser, is there a unified way to do this that doesn’t involve handling the various return types so that it’s transit-clj compatible? e.g. SciVar / SciFn.
Is the recommended way to handle this converting the return value to JSON? I’m displaying the results in a next-journal editor.
Side note: next journal handles this amazingly, but not sure of the technicals behind the implementation 😄
but to answer your question directly: SCI itself doesn't really help you with this. It does expose a type called sci.lang.IVar
which you can use in transit to print vars. There is no SciFn type.
@haywood btw, you create a ctx first with sci/init
and then use sci/eval-string*
- this skips some work initializing a context
Perhaps @mkvlr remembers the details of what's done in nextjournal for printing vars
yea, would be interested to see how they handle the ‘serde’, for reference the result for a function definition:
#'user/sup #object [user$sup 0x72b5f826 "user$sup@72b5f826"]
@haywood I checked nextjournal's code and I'm pretty certain that @mkvlr would allow me to share this.
(def sci-symbol (sci/eval-form ctx 'symbol))
(def sci-var? (sci/eval-form ctx 'var?))
(defn datafy-var
"Converts a sci var into something that's transit encodable.
See "
[v]
(if (sci-var? v)
^{:nextjournal/tag 'var} [(sci-symbol v) @v]
v))
(defn eval [code]
(datafy-var (sci/eval-string* ctx code)))
So what happens there is, just check if the top level thing is a var and transform it to something encodable
I would have guessed this myself, but I wondered if anything more complicated would happen for nested values, I guess not.
very generous of you to dig that up for me @borkdude, infinite thanks to you for everything you do
ah, right on: unwrap the var -> check if it’s a function -> $$$ yea this was stumping me for longer than I’d like to admit but this is working great! rendering in the browser-editor correctly and everything
@haywood great to hear - if your work is public, please do share and tell something about it (also if it's not public). we can also list it in the SCI readme.
It’s not public sadly, but I would love to write something about it! SCI is really the core engine behind an internal ETL tool that enables self service to folks who care about said data. Coincidentally, rewrite-clj will be the other set of shoulders we stand on for the ‘low-code’ interface so non-techy folks can define data transformations using form elements, and we translate that to Clojure functions that gets committed to source 🙂 I’m working on getting monetary support from the company, but no promises
You're of course welcome to sponsor as a company. I'm also available for commercial consulting/support around SCI if you need that. But it's great to hear what you're doing either way.
@haywood If you want, please share the above here too: https://github.com/babashka/sci/discussions/662 No strings attached. It helps me keep track of how people are using my projects.
Other SCI users have posted here before: https://github.com/babashka/babashka/discussions/1026