Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created June 18, 2011 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgrand/1033467 to your computer and use it in GitHub Desktop.
Save cgrand/1033467 to your computer and use it in GitHub Desktop.
(defmacro if-let
"A variation on if-let where all the exprs in the bindings vector must be true and also supports :let."
([bindings then]
`(if-let ~bindings ~then nil))
([bindings then else]
(if (seq bindings)
(if (= :let (bindings 0))
`(let ~(bindings 1)
(if-let ~(subvec bindings 2) ~then ~else))
`(let [test# ~(bindings 1)]
(if test#
(let [~(bindings 0) test#]
(if-let ~(subvec bindings 2) ~then ~else)))))
then)))
(defmacro when-let
"A variation on when-let where all the exprs in the bindings vector must be true and also supports :let."
[bindings & body]
`(if-let ~bindings (do ~@body)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment