-
-
Save fogus/1442920 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 let? | |
[bindings & body] | |
(let [[bind [kwd pred & more]] (split-with (complement #{:ensure}) bindings)] | |
`(let [~@bind] | |
~@(cond (and kwd more) [`(when ~pred (check-let [~@more] ~@body))] | |
kwd [`(when ~pred ~@body)] | |
:else body)))) | |
(let? [x 100 | |
y 300 | |
:ensure (pos? y) | |
z (- y 250)] | |
z) | |
;; expands to | |
(let [x 100 | |
y 300] | |
(when (pos? y) | |
(let [z (- y 250)] | |
z))) ;; => 50 | |
;; and returns 50. The following returns nil: | |
(let? [x 100 | |
y 300 | |
:ensure (neg? y) | |
z (- y 250)] | |
z) ;; => nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment