Skip to content

Instantly share code, notes, and snippets.

@BRonen
Created May 8, 2025 05:27
Show Gist options
  • Save BRonen/abcab52df2069d84c7e928f41e448788 to your computer and use it in GitHub Desktop.
Save BRonen/abcab52df2069d84c7e928f41e448788 to your computer and use it in GitHub Desktop.
cond-as-> macro definition
(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