macro | since v0.0-927 | imported clojure.core/cond | Edit |
(cond & clauses)
clauses
must be an even number of forms, ie: (cond t1 e1, t2 e2, t3 e3)
.
Each test t
is evaluated one at a time. If a test returns logical true, cond
evaluates and returns the corresponding expression e
and does not evaluate any
of the other tests or expressions.
It is idiomatic to provide a default case as the last test pair using the
keyword :else
(a keyword always evaluates to logical true).
(cond)
returns nil.
(def a 42)
(cond
(< a 10) "a is less than 10"
(= a 10) "a is 10"
(> a 10) "a is bigger than 10"
:else "a is not a number!")
;;=> "a is bigger than 10"
Takes a set of test/expr pairs. It evaluates each test one at a time. If a test returns logical true, cond evaluates and returns the value of the corresponding expr and doesn't evaluate any of the other tests or exprs. (cond) returns nil.
(defmacro cond
{:added "1.0"}
[& 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))))))