Skip to content

Instantly share code, notes, and snippets.

@fogus
Forked from sritchie/let.clj
Created December 7, 2011 14:04
Show Gist options
  • Save fogus/1442920 to your computer and use it in GitHub Desktop.
Save fogus/1442920 to your computer and use it in GitHub Desktop.
(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