-
-
Save aconbere/e1a2f003bfc1f6d6d782 to your computer and use it in GitHub Desktop.
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
(defmacro with-ns | |
"Evaluates f within ns. Calls (require 'ns) first." | |
[ns f] | |
`(do | |
(require '~ns) | |
(binding [*ns* (find-ns '~ns)] | |
(eval '~f)))) |
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
(defn trap-ns [ns f] | |
(if (ns-exists ns) | |
`(pig/with-ns ~ns ~f) | |
f)) | |
(defn trap* [keys values ns f] | |
(->> f | |
(trap-locals keys values) | |
(trap-ns ns))) | |
(defmacro trap | |
"Returns a form that, when evaluated, will reconsitiute f in namespace ns, in | |
the presence of any local bindings" | |
([f] `(trap '~(ns-name *ns*) ~f)) | |
([ns f] | |
(let [keys# (vec (keys &env))] | |
`(trap* '~keys# ~keys# ~ns '~f)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment