Created
July 2, 2011 01:37
-
-
Save nathanmarz/1059663 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-var-roots [bindings & body] | |
(let [settings (partition 2 bindings) | |
tmpvars (repeatedly (count settings) (partial gensym "old")) | |
vars (map first settings) | |
savevals (vec (mapcat (fn [t v] [t v]) tmpvars vars)) | |
setters (for [[v s] settings] `(set-var-root ~v ~s)) | |
restorers (map (fn [v s] `(set-var-root ~v ~s)) vars tmpvars) | |
] | |
`(let ~savevals | |
~@setters | |
(try | |
~@body | |
(finally | |
~@restorers)) | |
))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment