Created
May 14, 2015 16:28
-
-
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
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 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))) |
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
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