Last active
September 27, 2017 20:12
-
-
Save madstap/52f280f17289f1a1ec81f3c9f0b23778 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro when-letp | |
{:style/indent 2} | |
[pred bindings & body] | |
(if (empty? bindings) | |
`(do ~@body) | |
(let [[[binding expr] more] (split-at 2 bindings)] | |
`(let [temp# ~expr | |
pred# ~pred] | |
(when (pred# temp#) | |
(let [~binding temp#] | |
(when-letp pred# ~(vec more) ~@body))))))) | |
(comment | |
(let [x (calc-x)] | |
(when (pred? x) | |
(do-something-to x))) | |
(when-letp pred? [x (calc-x)] | |
(do-something-to x)) | |
(when-letp number? [x 42] | |
(inc x)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment