Skip to content

Instantly share code, notes, and snippets.

@mszajna
Last active March 24, 2021 22:31
Show Gist options
  • Save mszajna/592572d8ff41355ff6cdd94a2ba303f1 to your computer and use it in GitHub Desktop.
Save mszajna/592572d8ff41355ff6cdd94a2ba303f1 to your computer and use it in GitHub Desktop.
Proof of concept for REPL sessions with scoped context
(def ^:dynamic *depth* 0)
(defn repl-env [env]
(with-local-vars [ret nil]
(clojure.main/repl
:eval #(var-set ret (eval `(let [~@(mapcat identity env)] ~%)))
:prompt #(printf "%s%s=> " (ns-name *ns*) (apply str (repeat *depth* "="))))
@ret))
(defmacro cnt []
`(let [~'locals ~(->> &env keys (map (fn [n] [`(quote ~n) n])) (into {}))]
(binding [*depth* (inc *depth*)]
(repl-env ~'locals))))
; user=> (let [a 1] (cnt))
; user==> (inc a)
; 2
; user==> Ctrl+D
; 2
; user=> a
; user=> Unable to resolve symbol: a in this context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment