Last active
March 24, 2021 22:31
-
-
Save mszajna/592572d8ff41355ff6cdd94a2ba303f1 to your computer and use it in GitHub Desktop.
Proof of concept for REPL sessions with scoped context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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