Return first item in a map/list/sequence that satisfies a predicate
user=> (defn find-first
[f coll]
(first (filter f coll)))
#'user/find-first
user=> (find-first #(= % 1) [3 4 1])
1
Edit: A concurrency. :) No. It does not apply f
to the whole list. Only to the elements up to the first matching one due to laziness of filter
.
In your case, the idiom is
(some #{1} [1 2 3 4])
How it works: #{1} is a set literal. A set is also a function evaluating to its arg if the arg is present in the set and to nil otherwise. Any set element is a "truthy" value (well, except for a boolean false, but that's a rarity in a set). some
returns the return value of the predicate evaluated against the first collection member for which the result was truthy.