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.
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.
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"}
Alan
1 Like
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.