Created
October 31, 2018 19:41
-
-
Save RutledgePaulV/6b91519cb71cd659e85bb3d091f9a31d to your computer and use it in GitHub Desktop.
Like with-redefs but local to the thread. Doesn't require dynamic function vars.
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 *hook* false) | |
(defn hook [var f] | |
(alter-var-root var | |
(fn [original] | |
(fn [& args] | |
(if *hook* | |
(apply f args) | |
(apply original args)))))) | |
(defmacro with-redefs* | |
"Like with-redefs but scoped to only the | |
thread of execution." | |
[bindings & body] | |
`(let [m# ~(zipmap (take-nth 2 bindings) | |
(take-nth 2 (next bindings)))] | |
(doseq [[k# v#] m#] (hook k# v#)) | |
(binding [*hook* true] ~@body))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment