Skip to content

Instantly share code, notes, and snippets.

@philjackson
Last active December 1, 2015 08:39
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 philjackson/64f51d803f585330bb20 to your computer and use it in GitHub Desktop.
Save philjackson/64f51d803f585330bb20 to your computer and use it in GitHub Desktop.
(defmacro condev
"Takes clauses in the same style as `cond` but, if the left side is
truthy, will execute the right side of the expressions as a function
which gets the result of evaluation of the left side."
[& clauses]
(assert (even? (count clauses)))
(let [pstep (fn [[test step]] `(when-let [r# ~test] (~step r#)))]
`(list ~@(map pstep (partition 2 clauses)))))
@philjackson
Copy link
Author

(condev
 (+ 1 4) #(prn (+ 100 %))
 nil     #(prn "won't happen")
 (+ 1 1) #(prn (+ 200 %))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment