View Subcategory
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.
clojure
(every? #(> % 1) [2 3 4])
cpp
template <typename InputIterator, typename Predicate>
bool match_all(InputIterator first, InputIterator last, Predicate pred)
{
return find_if(first, last, !pred(_1)) == last;
}
bool match_all(InputIterator first, InputIterator last, Predicate pred)
{
return find_if(first, last, !pred(_1)) == last;
}
erlang
Result = lists:all(Pred, List).
fantom
echo([2,3,4].all{ it>1 })
fsharp
let rec IsAll predicate source =
let mutable acc = true
for e in source do
acc <- acc && (predicate e)
acc
let mutable acc = true
for e in source do
acc <- acc && (predicate e)
acc
groovy
[2,3,4].every{it > 1}
haskell
all (> 1) [2, 3, 4]
all (> 1) [2, 3, 4]
ocaml
(* from the interactive loop *)
# List.for_all (fun x -> x > 1) [2; 3; 4] ;;
- : bool = true
# List.for_all (fun x -> x > 1) [2; 3; 4] ;;
- : bool = true
python
all(x > 1 for x in [2,3,4])
ruby
[2, 3, 4].all? { |x| x > 1 }
scala
List(2, 3, 4).forall { _ > 1 }
List(2, 3, 4).forall { x => x > 1 }
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.
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])
(defn any? [pred coll]
((complement not-any?) pred coll))
(any? #(> % 3) [2 3 4])
(some #(> % 3) [2 3 4])
cpp
template <typename InputIterator, typename Predicate>
bool match_any(InputIterator first, InputIterator last, Predicate pred)
{
return find_if(first, last, pred) != last;
}
bool match_any(InputIterator first, InputIterator last, Predicate pred)
{
return find_if(first, last, pred) != last;
}
erlang
Result = lists:any(Pred, List).
fantom
echo([2,3,4].any{ it==4 })
fsharp
let rec IsAny predicate source =
match source with
| [] -> false
| h::t ->
if (predicate h) then true
else (IsAny predicate t )
match source with
| [] -> false
| h::t ->
if (predicate h) then true
else (IsAny predicate t )
groovy
[2,3,4].any{it > 3}
haskell
any (> 1) [1, 2, 3]
ocaml
(* from the interactive loop: *)
# List.exists (fun x -> x > 3) [2; 3; 4] ;;
- : bool = true
# List.exists (fun x -> x > 3) [2; 3; 4] ;;
- : bool = true
python
any(x > 3 for x in [2, 3, 4])
ruby
[2, 3, 4].any? { |x| x > 3 }
scala
List(2, 3, 4).exists { _ > 3 }
List(2, 3, 4).exists { x => x > 3 }
