Created
May 8, 2025 05:27
-
-
Save BRonen/abcab52df2069d84c7e928f41e448788 to your computer and use it in GitHub Desktop.
cond-as-> macro definition
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 cond-as-> | |
[value sym & clauses] | |
(when (odd? (count clauses)) | |
(throw (IllegalArgumentException. "cond-as-> requires an even number of clauses"))) | |
(let [steps (partition 2 clauses)] | |
(loop [val value, steps steps] | |
(if (empty? steps) | |
val | |
(let [[test expr] (first steps) | |
next-val `(let [~sym ~val] | |
(if ~test ~expr ~sym))] | |
(recur next-val (rest steps))))))) | |
;; (cond-as-> 1 x true (+ x x) false (* x 2) true (- x 3)) | |
;; => -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment