Today we look at a few functions in Clojure that take predicates and look at collections: any?, every?, not-every? and not-any?
any? seems to return true for any argument. According to the docs, it is used for clojure.spec. Maybe when I learn more about spec, that will make sense. You would think a function called “any?” would be useful if you had a collection of numbers, and wanted to know if any of them are odd, or greater than 10, or something like that. Perhaps they could have given it a better name than “any?”
every?, not-any? and not-every? behave as you would expect them to. You can always simulate “intuituve-any?” by calling “not-any?” and then calling “not”.
user> (def evens [2 4 6 8]) #'user/evens user> (def evens-with-9 [2 3 4 6 8 9]) #'user/evens-with-9 user> (every? even? evens) true user> (every? even? evens-with-9) false user> (not-every? even? evens) false user> (not-every? even? evens-with-9) true user> (not-every? odd? evens) true user> (not-every? odd? evens-with-9) true user> (not-any? odd? evens) true user> (not-any? odd? evens-with-9) false user> (any? 'g) true user> (any? nil) true user> (not (not-any? odd? evens)) false user> (not (not-any? odd? evens-with-9)) true
You’re welcome.
Image from the Rheinau Psalter, a 13th century manuscript housed at Central Library of Zurich. Image from e-Codices. This image is assumed to be allowed under Fair Use.