Cond in clojure

What is the correct way in Clojure to do an if then else if then else if … ?
In Common Lisp we have a (cond ...) for this idiom, and also a (case ...) if it is simply a series of equivalence or membership checks.

cool. I found the docs for clojure.core with a lot of familiar functions including case and cond. is handy for looking up all the essential constructs.

1 Like

Similar to what @crispinb said, I find the ClojureScript cheatsheet a helpful reference. It’s obviously for ClojureScript rather than JVM Clojure but most functions are the same and the site itself I find well designed and easy to use.

I always find myself reaching for the cheatsheet - all the good stuff is there. :slight_smile:


If you don’t mind using a library, I am quite fond of the cond-it-> macro in the Tupelo Clojure library. Enjoy!

(cond-it-> & forms)

A threading macro like cond-> that always uses the symbol ‘it’ as the placeholder for the next threaded value. Works in both the conditional form and the value form:

(let [params {:a 1 :b 1 :c nil :d nil}]
  (cond-it-> params
    (:a it)        (update it :b inc)
    (= (:b it) 2)  (assoc it :c "here")
    (:c it)        (assoc it :d "again")))

;=> {:a 1, :b 2, :c "here", :d "again"}


1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.