View Problem

Test if a condition holds for all items of a list

Given a list, test if a certain logical condition (i.e. predicate) holds for all items of the list.
DiskEdit
clojure
(every? #(> % 1) [2 3 4])
ExpandDiskEdit
cpp boost
template <typename InputIterator, typename Predicate>
bool match_all(InputIterator first, InputIterator last, Predicate pred)
{
return find_if(first, last, !pred(_1)) == last;
}
DiskEdit
erlang
Result = lists:all(Pred, List).
ExpandDiskEdit
fantom
echo([2,3,4].all{ it>1 })
DiskEdit
fsharp fsharp
let rec IsAll predicate source =
let mutable acc = true
for e in source do
acc <- acc && (predicate e)
acc
DiskEdit
groovy
[2,3,4].every{it > 1}
DiskEdit
haskell

all (> 1) [2, 3, 4]
DiskEdit
ocaml
(* from the interactive loop *)
# List.for_all (fun x -> x > 1) [2; 3; 4] ;;
- : bool = true
DiskEdit
python 2.6
all(x > 1 for x in [2,3,4])
DiskEdit
ruby
[2, 3, 4].all? { |x| x > 1 }
ExpandDiskEdit
scala
List(2, 3, 4).forall { _ > 1 }
ExpandDiskEdit
scala
List(2, 3, 4).forall { x => x > 1 }

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