控制流 / 条件语句 / switch/case 语句

(defrecord Tiger [age name])

;; there is no separate "when" guard on a branch: a cond branch is an
;; ordinary expression, so the guard is just another "and" inside it
(defn describe [x]
  (cond
    (and (int? x) (> x 1000000)) "Big number"
    (and (string? x) (< (count x) 256)) "Short string"
    (and (instance? Tiger x) (> (:age x) 12)) "Old tiger"
    (and (instance? Tiger x) (> (count (:name x)) 10)) "Long tiger name"
    (instance? Tiger x) "Short tiger name"
    :else "Unknown type"))

(def result (describe (->Tiger 15 "Sherkhan")))
;; result is "Old tiger"
(println "result is" (pr-str result))

(println (describe 1000001))
(println (describe "test"))
(println (describe (->Tiger 3 "Sherkhan-the-second")))
(println (describe :cat))

;; condp shortens the chain when every branch asks the same question
(println (condp instance? (->Tiger 3 "Kitty")
           String "a string"
           Tiger "a tiger"
           "something else"))