(case e & clauses)
Takes an expression, and a set of clauses. Each clause can take the form of either: test-constant result-expr (test-constant1 ... test-constantN) result-expr The test-constants are not evaluated. They must be compile-time literals, and need not be quoted. If the expression is equal to a test-constant, the corresponding result-expr is returned. A single default expression can follow the clauses, and its value will be returned if no clause matches. If no default expression is provided and no clause matches, an IllegalArgumentException is thrown. Unlike cond and condp, case does a constant-time dispatch, the clauses are not considered sequentially. All manner of constant expressions are acceptable in case, including numbers, strings, symbols, keywords, and (Clojure) composites thereof. Note that since lists are used to group multiple constants that map to the same expression, a vector can be used to match a list if needed. The test-constants need not be all of the same type.
(let [mystr "hello"]
(case mystr
"" 0
"hello" (count mystr)))
;;=> 5
(let [mystr "no match"]
(case mystr
"" 0
"hello" (count mystr)))
;; No matching clause: no match
;; [Thrown class java.lang.IllegalArgumentException]
(let [mystr "no match"]
(case mystr
"" 0
"hello" (count mystr)
"default"))
;;=> "default"
;; You can give multiple values for the same condition by putting
;; those values in a list.
(case 'y
(x y z) "x, y, or z"
"default")
;;=> "x, y, or z"
(let [myseq '(1 2)]
(case myseq
(()) "empty seq"
((1 2)) "my seq"
"default"))
;;=> "my seq"
;; "The test-constants are not evaluated.They must be compile-time
;; literals, and need not be quoted."
(let [myvec [1 2]]
(case myvec
[] "empty vec"
(vec '(1 2)) "my vec"
"default"))
;;=> "default"