View Problem

Test if a condition holds for any items of a list

Given a list, test if a certain logical condition (i.e. predicate) holds for any items of the list.
DiskEdit
clojure
; The standard library in Clojure has "not-any?" but (oddly enough) no "any?"
(defn any? [pred coll]
((complement not-any?) pred coll))

(any? #(> % 3) [2 3 4])
DiskEdit
clojure
(some #(> % 3) [2 3 4])
ExpandDiskEdit
cpp boost
template <typename InputIterator, typename Predicate>
bool match_any(InputIterator first, InputIterator last, Predicate pred)
{
return find_if(first, last, pred) != last;
}
DiskEdit
erlang
Result = lists:any(Pred, List).
ExpandDiskEdit
fantom
echo([2,3,4].any{ it==4 })
DiskEdit
fsharp fsharp
let rec IsAny predicate source =
match source with
| [] -> false
| h::t ->
if (predicate h) then true
else (IsAny predicate t )
DiskEdit
groovy
[2,3,4].any{it > 3}
DiskEdit
haskell
any (> 1) [1, 2, 3]
DiskEdit
ocaml
(* from the interactive loop: *)
# List.exists (fun x -> x > 3) [2; 3; 4] ;;
- : bool = true
DiskEdit
python 2.6
any(x > 3 for x in [2, 3, 4])
DiskEdit
ruby
[2, 3, 4].any? { |x| x > 3 }
ExpandDiskEdit
scala
List(2, 3, 4).exists { _ > 3 }
ExpandDiskEdit
scala
List(2, 3, 4).exists { x => x > 3 }

Submit a new solution for clojure, cpp, erlang, fantom ...