Skip to content

Instantly share code, notes, and snippets.

@Hendekagon
Created April 14, 2016 07:56
Show Gist options
  • Save Hendekagon/9ff9ee6d44a92e9066a5dd19b9bc7f31 to your computer and use it in GitHub Desktop.
Save Hendekagon/9ff9ee6d44a92e9066a5dd19b9bc7f31 to your computer and use it in GitHub Desktop.
explanatory-cond->
(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