Skip to content

Instantly share code, notes, and snippets.

@oliyh
Created May 14, 2015 16:28
Show Gist options
  • Save oliyh/bcd5b89ddce2ff79d876 to your computer and use it in GitHub Desktop.
Save oliyh/bcd5b89ddce2ff79d876 to your computer and use it in GitHub Desktop.
condas-> - A Clojure macro combining cond-> and as-> to give more flexibility in the test and step forms
(defmacro condas->
"A mixture of cond-> and as-> allowing more flexibility in the test and step forms"
[expr name & clauses]
(assert (even? (count clauses)))
(let [pstep (fn [[test step]] `(if ~test ~step ~name))]
`(let [~name ~expr
~@(interleave (repeat name) (map pstep (partition 2 clauses)))]
~name)))
user> (condas-> {} arg
(empty? arg) (assoc arg :foo :bar)
(:foo arg) (assoc arg :baz :buzz)
(:foos arg) (assoc arg :ball true))
{:baz :buzz, :foo :bar}
user> (condas-> {:foos true} arg
(empty? arg) (assoc arg :foo :bar)
(:foo arg) (assoc arg :baz :buzz)
(:foos arg) (assoc arg :ball true))
{:ball true, :foos true}
user>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment