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 | |
) |
Here is the new if-let* imp:
(defmacro if-let*
([bindings then]
`(if-let* ~bindings ~then nil))
([bindings then else]
(if (seq bindings)
`(if-let [~(first bindings) ~(second bindings)]
(if-let* ~(drop 2 bindings) ~then ~else)
~(if-not (second bindings) else))
then)))
(if-let* [a 1
b (+ a 1) ]
b)
;;=> 2
(if-let* [a 1
b (+ a 1)
c false] ;;false or nil - does not matter
b
a)
;;=> 1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just use recursion!
This way you don't evaluate any of the test expressions more than once, and you don't lose their values either.