Search code examples
clojure

How to exit a when a function mapped over a collection first returns true?


I want to map a function that returns a boolean over a collection and exit with true on the first true match... how would I do that?

(map #(true? %) [false false true false])

returns

=> (false false true false)

How would I simply get true?


Solution

  • (some pred coll) returns the first truthy (pred x) for x in coll, or nil if no such truthy value is found. Note that in Clojure, all values are truthy except nil and false. For your example:

    (some identity [false false true false])
    => true