Clojure's `and` combines conditions while preserving short-circuit evaluation. It evaluates expressions from left to right and stops at the first `false` or `nil`; otherwise, it returns the final expression's value rather than necessarily returning a Boolean. section 3.2.2
`and` accepts zero or more expressions:
- With no arguments, it returns `true`. section 3.2.2
- With one argument, it returns that argument. section 3.2.2
- It returns `false` when an expression produces `false`, and `nil` when an expression produces `nil`. section 3.2.2
- If every expression is logically true, it returns the last expression's value. section 3.2.2
Guarding a conditional
Use `and` to require several conditions before performing an action: (let [probe {:temp 150 :rpm "max"}] (when (and (> (:temp probe) 120) (= (:rpm probe) "max")) (println "Too hot, going protection mode."))) ;; Too hot, going protection mode. The second test is skipped when the temperature test is false. This makes `and` useful for guarding operations that should run only after earlier checks succeed. section 3.2.2
Guarding nil-sensitive work
Because only `false` and `nil` are falsey in Clojure, `and` can protect later expressions from a missing value: (defn path [s] (let [s (and s (.trim s))] (and (seq s) (subs s 0 (.lastIndexOf s "/"))))) (path "/tmp/exp/lol.txt") ;; "/tmp/exp" (path "") ;; nil (path nil) ;; nil section 3.2
Table 3.1. Common Clojure values and their boolean translation
| Description |
Example |
Boolean Value |
| Number zero |
|
|
| Empty string |
|
|
| Empty list |
|
|
| List of nil |
|
|
| Nil |
|
|
Use the truthiness table to check how numbers, empty collections, empty strings, and `nil` behave inside `and` conditions. section 3.2






