Last active
July 26, 2016 23:58
-
-
Save pingles/5150585 to your computer and use it in GitHub Desktop.
Clojure if-let accepting multiple bindings
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
(ns if-let-multi-bind.core) | |
(defmacro if-let* | |
([bindings then] | |
`(if-let* ~bindings ~then nil)) | |
([bindings then else & oldform] | |
(let [test (cons #'and (map last (partition 2 bindings)))] | |
`(if ~test | |
(let ~bindings | |
~then) | |
~else)))) | |
(comment | |
(if-let* [a 1 | |
b 3] | |
(+ a b)) | |
;; 4 | |
(if-let* [a 1 | |
b nil] | |
(+ a b)) | |
;; nil | |
(def x 1) | |
(def y false) | |
(if-let* [a x | |
b y] | |
[a b]) | |
;; nil | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the new if-let* imp: