Skip to content

Instantly share code, notes, and snippets.

@amalloy
Created March 18, 2011 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalloy/875767 to your computer and use it in GitHub Desktop.
Save amalloy/875767 to your computer and use it in GitHub Desktop.
Which do you like better? Will the second have issues if the user has an unusual ns declaration, in which clojure.core/cond is not mapped?
(defmacro cond [& clauses]
(if-not (even? (count clauses))
(throw (IllegalArgumentException. "cond requires an even number of forms"))
(reduce (fn [else [test then]]
`(if ~test ~then ~else))
nil
(reverse (partition 2 clauses)))))
(defmacro cond [& clauses]
(when clauses
(list 'if (first clauses)
(if (next clauses)
(second clauses)
(throw (IllegalArgumentException.
"cond requires an even number of forms")))
(cons 'clojure.core/cond (next (next clauses))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment