sci

https://github.com/babashka/SCI - also see #babashka and #nbb
denik 2021-03-04T18:09:48.056800Z

Is there a way to keep ns transitions around between evaluations?

(def sci-ctx (sci/init {}))

(defn eval-form [form]
  (sci/with-bindings {sci/ns @sci/ns}
                     (sci/eval-form sci-ctx form)))

(eval-form '(do (in-ns 'foo)
                (ns-name *ns*)))
;=> foo

(eval-form '(ns-name *ns*))
;=> user ;; not foo anymore!

borkdude 2021-03-04T18:54:38.057700Z

@denik with-bindings implies that the binding is restored when the body has ended. If you want to implement some kind of REPL then it's best to have the loop inside the with-bindings

denik 2021-03-04T18:56:24.059100Z

@borkdude I don’t have traditional REPL but more something like REP where a client sends new forms and triggers the next evaluation cycle

denik 2021-03-04T18:56:52.059800Z

I tried wrapping the eval-form function inside with-bindings but that threw errors

borkdude 2021-03-04T18:56:53.059900Z

So you want to store the ns state per client?

borkdude 2021-03-04T18:57:13.060200Z

Do you have multiple clients with different states?

denik 2021-03-04T18:57:29.060500Z

Only one client

borkdude 2021-03-04T18:58:25.061700Z

You could store the ns in some kind of atom and bind it the next time

borkdude 2021-03-04T18:58:50.062300Z

(reset! the-current-ns (sci/eval-string* ctx "*ns*"))

denik 2021-03-04T18:59:14.062800Z

let me try that

borkdude 2021-03-04T18:59:15.063Z

and then (sci/with-bindings {sci/ns (or @the-current-ns sci/ns)} ...)

denik 2021-03-04T19:01:49.063600Z

nice, might have found a simpler solution:

(def sci-ctx (sci/init {}))

(def current-ns (atom @sci/ns))

(defn eval-form [form]
  (sci/with-bindings {sci/ns @current-ns}
                     (let [ret (sci/eval-form sci-ctx form)]
                       (reset! current-ns @sci/ns)
                       ret)))

(eval-form '(do (in-ns 'foo)
                (ns-name *ns*)))
;=> foo
(eval-form '(ns-name *ns*))
;=> foo

denik 2021-03-04T19:02:06.063900Z

thank you @borkdude!

borkdude 2021-03-04T19:02:07.064Z

:thumbsup:

denik 2021-03-04T19:07:52.065400Z

nice thanks for the reference