Clojure not nil check
After Clojure 1.6 you can use some?
:
(some? :foo) => true
(some? nil) => false
This is useful, eg, as a predicate:
(filter some? [1 nil 2]) => (1 2)
Another way to define not-nil?
would be using the complement
function, which just inverts the truthyness of a boolean function:
(def not-nil? (complement nil?))
If you have several values to check then use not-any?
:
user> (not-any? nil? [true 1 '()])
true
user> (not-any? nil? [true 1 nil])
false
If you are not interested in distinguishing false
from nil
, you can just use the value as the condition:
(if value1
"value1 is neither nil nor false"
"value1 is nil or false")