Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created October 4, 2013 19:18
Show Gist options
  • Save jcromartie/6831218 to your computer and use it in GitHub Desktop.
Save jcromartie/6831218 to your computer and use it in GitHub Desktop.
A macro that wraps values in a binding form with `future` and transparently `deref` them in the body.
(defmacro let-futures
[bindings & body]
(let [binding-pairs (partition 2 bindings)
future-syms (set (map first binding-pairs))
binding-vector (vec (mapcat (fn [[l r]] [l (list 'future r)]) binding-pairs))
body-code (clojure.walk/walk
(fn derefify [x]
(if (future-syms x)
(list 'clojure.core/deref x)
(clojure.walk/walk derefify identity x)))
identity
body)]
`(let ~binding-vector ~@body-code)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment