Created
February 3, 2010 00:43
-
-
Save michalmarczyk/293212 to your computer and use it in GitHub Desktop.
Scheme-style cond macro for Clojure
This file contains hidden or 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 scond | |
"Scheme style cond macro. | |
Use :>> instead of => and :else instead of else." | |
[& clauses] | |
(when-let [clause (first clauses)] | |
(cond | |
(= (first clause) :else) (if (next clauses) | |
(throw (IllegalArgumentException. | |
(str ":else clause must be last in scond: " | |
clause))) | |
(if (not= (count clause) 2) | |
(throw (IllegalArgumentException. | |
(str ":else clause must have exactly two components: " | |
clause))) | |
(second clause))) | |
(= (second clause) :>>) (if (== (count clause) 3) | |
`(if-let [x# ~(first clause)] | |
(~(nth clause 2) x#) | |
(scond ~@(rest clauses))) | |
(throw (IllegalArgumentException. | |
(str ":>> clause must have exactly three components: " | |
clause)))) | |
(== (count clause) 1) `(if-let [x# ~(first clause)] | |
x# | |
(scond ~@(rest clauses))) | |
(== (count clause) 2) `(if ~(first clause) | |
~(second clause) | |
(scond ~@(rest clauses))) | |
:else (throw (IllegalArgumentException. | |
(str "malformed scond clause: " clause)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment