Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Clojure not have an any? or any-pred? function?

Tags:

clojure

It seems like I must be missing some obvious commonly used idiom, but the absence of these two functions seems a bit perplexing to me. There is some but it returns nil instead of False, why not an any? function?

like image 726
Kamil Kisiel Avatar asked Feb 28 '12 23:02

Kamil Kisiel


People also ask

Is nil false in Clojure?

The values nil and false are treated as logically false in Clojure. All other values are treated as logically true.

What is clojure predicate?

Advertisements. Predicates are functions that evaluate a condition and provide a value of either true or false. We have seen predicate functions in the examples of the chapter on numbers.


1 Answers

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

like image 118
Arthur Ulfeldt Avatar answered Oct 29 '22 16:10

Arthur Ulfeldt