clojure

Clojure equivalent to Python's "any" and "all" functions?


Are there built in functions in Clojure similar to Python's any and all functions?

For example, in Python, it's all([True, 1, 'non-empty string']) == True.


Solution

  • (every? f data) [docs] is the same as all(f(x) for x in data) .

    (some f data) [docs] is like any(f(x) for x in data) except that it returns the value of f(x) (which must be truthy), instead of just true.

    If you want the exact same behaviour as in Python, you can use the identity function, which will just return its argument (equivalent to (fn [x] x)).

    user=> (every? identity [1, true, "non-empty string"])
    true
    user=> (some identity [1, true "non-empty string"])
    1
    user=> (some true? [1, true "non-empty string"])
    true