Fork me on GitHub
#sci
<
2022-01-14
>
haywood20:01:52

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 😄

borkdude20:01:18

@mkvlr I think you have some experience with this right? ;)

borkdude20:01:28

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.

haywood20:01:32

ah, right on

borkdude20:01:45

@haywood btw, you create a ctx first with sci/init and then use sci/eval-string* - this skips some work initializing a context

haywood20:01:49

oh good to know, I’ll revert to that impl!

haywood20:01:16

you’re like, a mind reader… can you see my screen?

borkdude20:01:20

hmm, the sci.lang.IVar stuff isn't exposed for CLJS I see now.

borkdude20:01:58

Perhaps @mkvlr remembers the details of what's done in nextjournal for printing vars

haywood20:01:34

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"]

borkdude20:01:15

does transit-cljs write-handler support protocols?

borkdude21:01:16

@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)))

🔥 1
haywood21:01:35

giving it a shot now…

borkdude21:01:41

So what happens there is, just check if the top level thing is a var and transform it to something encodable

borkdude21:01:03

I would have guessed this myself, but I wondered if anything more complicated would happen for nested values, I guess not.

borkdude21:01:56

although if that is a concern you can also postwalk your results

borkdude21:01:10

and perhaps transit has some :default writer thing for types it doesn't know

haywood21:01:04

sweet! work’s great 🙂

haywood21:01:11

they must be handling functions separately somewhere too

haywood21:01:21

Caused by: java.lang.Exception: Not supported: class sci.impl.fns$fun$arity_1__18434

haywood21:01:45

very generous of you to dig that up for me @borkdude, infinite thanks to you for everything you do

borkdude21:01:37

Oh, you are in Java. I thought you were in the browser :)

borkdude21:01:59

There you can make a writer for clojure.lang.Fn

borkdude21:01:04

user=> (instance? clojure.lang.Fn (sci/eval-string "(fn [])"))
true

haywood22:01:13

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 excited

borkdude22:01:47

@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.

haywood22:01:38

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

👍 1
borkdude22:01:17

Exciting :)

borkdude22:01:55

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.

haywood22:01:37

Totally, I’ll keep that in mind!

borkdude22:01:54

@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.

haywood22:01:10

awesome! this is great, I’ll add to it shortly. company is starting the new year with a re-org so I’m fighting for this project’s life. but once that is certain I’ll happily update this issue 🙂

👍 1