Difference between Case, Cond and Condp

(defn case-test-1
[n]
(case n
1 "n is 1"
2 "n is 2"
"n is other"))

Cond

When you want to do a similar thing to case but want to write your own test case rather than =, you can use cond. You can write a different test case in each branch with cond.

user=> (defn cond-test
[n]
(cond
(= n 1) "n is 1"
(and (> n 3) (< n 10)) "n is over 3 and under 10"
:else "n is other"))

Condp

You can use a predicate with condp for condition. In this case contains? is the predicate.

user=> (defn condp-test-2
[n]
(condp contains? n
#{1 2 3} "n is either 1 or 2 or 3"
"n is not 1 or 2 or 3"))
#'user/condp-test-2
user=> (println (condp-test-2 2))
n is either 1 or 2 or 3
nil
user=> (println (condp-test-2 5))
n is not 1 or 2 or 3
nil

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store