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 | |
) |
Maybe my suggestion to use and
was actually a bad idea since we "lose" the values of resulting from the tests... I'm starting to think the nested if-let
approach might actually be necessary, at least if:
- Tests should be evaluated lazily, ie. first failure should short-circuit
- Tests should only be evaluated once
But there is most probably other possibilities/solutions too...
Just use recursion!
(defmacro if-let*
([bindings then]
`(if-let* ~bindings ~then nil))
([bindings then else]
(if (empty? bindings)
then
`(if-let ~(vec (take 2 bindings))
(if-let* ~(drop 2 bindings) ~then ~else)
~else))))
This way you don't evaluate any of the test expressions more than once, and you don't lose their values either.
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
Cool- the gist now uses
and
. Final step is to avoid duplicate evaluation of the test expressions. I wondered whether a sequence that had already been realised wouldn't require the re-evaluation of the tests but it doesn't :(Any thoughts?