Snippets and other bits

condev - like cond but different

Jul 26, 2021

Hopefully the docstring explains this well enough:

(defmacro condev
  "Takes clauses in the same style as `cond` but, if the left side is
  truthy, will execute the right side of the expressions as a function
  which gets the result of evaluation of the left side."
  [& clauses]
  (assert (even? (count clauses)))
  (let [pstep (fn [[test step]] `(when-let [r# ~test] (~step r#)))]
    `(list ~@(map pstep (partition 2 clauses)))))

Usage:

(condev
 (+ 1 4) #(prn (+ 100 %))
 nil     #(prn "won't happen")
 (+ 1 1) #(prn (+ 200 %))

← Back to root