Skip to content

Instantly share code, notes, and snippets.

@commondatageek
Last active October 13, 2015 19:02
Show Gist options
  • Save commondatageek/91a4742f30becf5890ec to your computer and use it in GitHub Desktop.
Save commondatageek/91a4742f30becf5890ec to your computer and use it in GitHub Desktop.
while-let: Keep looping while let bindings are truthy
(defmacro while-let
"While all bindings are truthy, execute body. Useful for draining
channels, buffers, anything that can be read, etc."
[bindings & body]
(let [pairs (partition 2 bindings)
test-exprs (map second pairs)
test-vals (map first pairs)]
`(loop ~bindings
(if (and ~@test-vals)
(do
~@body
(recur ~@test-exprs))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment