Skip to content

Instantly share code, notes, and snippets.

@Solaxun
Last active July 20, 2020 18:17
Show Gist options
  • Save Solaxun/70a06643b990b4075a41bb2eb27d7426 to your computer and use it in GitHub Desktop.
Save Solaxun/70a06643b990b4075a41bb2eb27d7426 to your computer and use it in GitHub Desktop.
At runtime, the function created by the macroexpansion captures lexical scope from wherever it's called, and is stored in the atom.
(def protos (atom nil))
(defn build-func [[name args body]]
(list 'fn name args body))
(defmacro bleh [fbody]
`(let [f# ~(build-func fbody)]
(do (reset! protos f#) f#)))
(macroexpand-1 '(bleh (foo [y] (+ x y))))
(let [x 100
func (bleh (foo [y] (+ x y)))]
(func 15))
;; closure created above stored in atom (i.e. protocol map)
(@protos 80)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment