Skip to content

Instantly share code, notes, and snippets.

@jaidetree
Created February 22, 2022 16:34
Show Gist options
  • Save jaidetree/80d428cf65b5006eb60d90caefefbcd5 to your computer and use it in GitHub Desktop.
Save jaidetree/80d428cf65b5006eb60d90caefefbcd5 to your computer and use it in GitHub Desktop.
(defmacro with-redefs-async
[bindings & body]
(let [names (take-nth 2 bindings)
vals (take-nth 2 (drop 1 bindings))
orig-val-syms (map (comp gensym #(str % "-orig-val__") name) names)
temp-val-syms (map (comp gensym #(str % "-temp-val__") name) names)
binds (map vector names temp-val-syms)
resets (reverse (map vector names orig-val-syms))
bind-value (fn [[k v]] (list 'set! k v))]
`(let [~@(interleave orig-val-syms names)
~@(interleave temp-val-syms vals)]
~@(map bind-value binds)
(promesa.core/-> (promesa.core/do! ~@body)
(promesa.core/finally
(fn []
~@(map bind-value resets)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment