Skip to content

Instantly share code, notes, and snippets.

@madstap
Last active September 27, 2017 20:12
Show Gist options
  • Save madstap/52f280f17289f1a1ec81f3c9f0b23778 to your computer and use it in GitHub Desktop.
Save madstap/52f280f17289f1a1ec81f3c9f0b23778 to your computer and use it in GitHub Desktop.
(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