Skip to content

Instantly share code, notes, and snippets.

@aconbere
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aconbere/e1a2f003bfc1f6d6d782 to your computer and use it in GitHub Desktop.
Save aconbere/e1a2f003bfc1f6d6d782 to your computer and use it in GitHub Desktop.
(defmacro with-ns
"Evaluates f within ns. Calls (require 'ns) first."
[ns f]
`(do
(require '~ns)
(binding [*ns* (find-ns '~ns)]
(eval '~f))))
(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