Fork me on GitHub
#sci
<
2021-08-27
>
cap10morgan16:08:20

just started playing w/ sci. so dang cool. anyone have an example handy for defining a new fn in an sci ctx from a .cljc file (so it should work in both CLJ and CLJS). there's a hurdle my brain isn't clearing somehow...

borkdude16:08:52

@cap10morgan you can use (sci/copy-var the-cljc-var sci-ns)

borkdude16:08:12

where sci-ns is something created with sci/create-ns

cap10morgan16:08:32

well, it doesn't exist as a var yet 🙂 I want to create in the sci ctx only

borkdude16:08:37

e.g. (sci/copy-var inc (sci/create-ns 'clojure.core nil))

borkdude16:08:16

what I would do then is:

(defn my-new-fn [] ...)
and use copy-var on that, or just use my-new-fn directly

borkdude16:08:31

I don't understand what .cljc has to do with this :)

cap10morgan16:08:02

hopefully nothing! just mentioning in case the cljs side complicated anything 🙂

borkdude16:08:17

yes, some things work differently in CLJS so it's good that you mentioned that

borkdude16:08:27

but as for normal functions, I think it should just work

cap10morgan16:08:55

so, in this case I have user-supplied code at runtime that ideally would only become a fn bound to a var in sci, not in the outer clj(s) context

borkdude16:08:44

right. so you could do:

(def ctx (let [f (fn [] ...)] (sci/init {:namespaces {'my-sci-ns {'f f}}})))

borkdude17:08:58

and then (sci/eval-string* ctx "whatever")

cap10morgan17:08:09

ok... yep. and to add to an existing context... use merge-opts?

cap10morgan17:08:30

:thumbsup: thanks!

cap10morgan17:08:47

@borkdude does merge-opts add to the existing :namespaces and :allow values or do I need to do that myself first and pass in the whole new value for those?

cap10morgan17:08:35

(assuming I have :namespaces and :allow keys in my 2nd arg to it)

borkdude17:08:12

namespaces are merged for sure

borkdude17:08:18

allow: not sure :)

borkdude17:08:09

yes, they are added together

cap10morgan17:08:16

killer, thanks!

cap10morgan18:08:16

@borkdude if I wanted to replace some clojure.core fns w/ my own implementations in an sci ctx, would that be as simple as {:namespaces {'clojure.core map-of-my-replaced-syms-to-their-new-impls}}?

borkdude18:08:08

it might not work for some special forms or things that are baked in like defn