cond->

macrosince v0.0-1798imported clojure.core/cond->Edit
(cond-> expr & clauses)

Details:

Takes an expression and a set of test/form pairs. Threads expr (via ->) through each form for which the corresponding test expression is true.

Note that, unlike cond branching, cond-> threading does not short circuit after the first true test expression.


Examples:

(def a 12)
(cond-> a
  (> a 10) (str " is greater than 10")
  (< a 20) (str " and less than 20"))
;;=> "12 is greater than 10 and less than 20"

See Also:


Source docstring:
Takes an expression and a set of test/form pairs. Threads expr (via ->)
through each form for which the corresponding test
expression is true. Note that, unlike cond branching, cond-> threading does
not short circuit after the first true test expression.
Source code @ clojure:src/clj/clojure/core.clj
(defmacro cond->
  {:added "1.5"}
  [expr & clauses]
  (assert (even? (count clauses)))
  (let [g (gensym)
        steps (map (fn [[test step]] `(if ~test (-> ~g ~step) ~g))
                   (partition 2 clauses))]
    `(let [~g ~expr
           ~@(interleave (repeat g) (butlast steps))]
       ~(if (empty? steps)
          g
          (last steps)))))