Why does Clojure not have an any? or any-pred? function?
nil
evaluates to false. (if nil 1 2)
evaluates to 2.
some
returns the first element that satisfies the predicate. Anything that is not nil
or false
evaluates to true. So (if (some odd? [2 2 3]) 1 2)
evaluates to 1.
some
is considered to be the same as any?
would be if it existed. there is a closely named function not-any?
which just calls some
under the hood:
(source not-any?)
(def
...
not-any? (comp not some))
you could simply write any as:
(def any? (comp boolean some))
patches welcomed :) just fill out and mail in a contributor agreement first.
your point about the naming is especially true considering the not-any?
function has been included since 1.0
(defn any? [pred col] (not (not-any? pred col)))
(any? even? [1 2 3])
true
(any? even? [1 3])
false
I guess nobody got around to sending in the patch? (hint hint nudge nudge)
When using any code based on some
(not-any?
calls some under the hood) be careful to match the types of the pred and the col or use a pred that catches the type exceptions
(if (some odd? [2 2 nil 3]) 1 2)
No message.
[Thrown class java.lang.NullPointerException]
ps: this example is from clojure 1.2.1