Skip to content

Instantly share code, notes, and snippets.

@nathanmarz
Created July 2, 2011 01:37
Show Gist options
  • Save nathanmarz/1059663 to your computer and use it in GitHub Desktop.
Save nathanmarz/1059663 to your computer and use it in GitHub Desktop.
(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