Skip to content

Instantly share code, notes, and snippets.

@michalmarczyk
Created February 3, 2010 00:43
Show Gist options
  • Save michalmarczyk/293212 to your computer and use it in GitHub Desktop.
Save michalmarczyk/293212 to your computer and use it in GitHub Desktop.
Scheme-style cond macro for Clojure
(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