Created
April 14, 2016 07:56
-
-
Save Hendekagon/9ff9ee6d44a92e9066a5dd19b9bc7f31 to your computer and use it in GitHub Desktop.
explanatory-cond->
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
(defmacro explanatory-cond-> | |
"Adapted from Clojure's cond-> macro to keep a list of conditions and results" | |
[expr & clauses] | |
(assert (even? (count clauses))) | |
(let [g (gensym) | |
pstep | |
(fn [[test step]] | |
`(if ~test | |
[(-> (if (vector? (first ~g)) (ffirst ~g) (first ~g)) ~step) | |
(cons [(quote ~test) (quote ~step) (first ~g)] (second ~g))] | |
~g) | |
) | |
] | |
`(let [~g [~expr nil] | |
~@(interleave (repeat g) (map pstep (partition 2 clauses)))] | |
~g))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment