Skip to content

Instantly share code, notes, and snippets.

@crocket
Created June 5, 2015 04:36
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 crocket/57bfd42f4b74f90ad69d to your computer and use it in GitHub Desktop.
Save crocket/57bfd42f4b74f90ad69d to your computer and use it in GitHub Desktop.
if-let-all clojure macro
(defmacro if-let-all
"if-let-all evaluates every local binding sequentially and evaluates true-case only if every local binding is a truthy value.
true-case has access to all local bindings, but false-case doesn't have access to local bindings."
[bindings true-case false-case]
(let [pairs (partition 2 bindings)
names (mapv first pairs)
exprs (map second pairs)
exprs-in-if-let (fn self [[name1 & more-names] [expr1 & more-exprs]]
`(if-let [~name1 ~expr1]
~(if more-names
(self more-names more-exprs)
names)))
things (exprs-in-if-let names exprs)]
`(if-let [~names ~things]
~true-case
~false-case)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment