HH\Lib\C\every

Meta Engineer?

This is available as C\every in the www repository.

Returns true if the given predicate returns true for every element of the given Traversable

namespace HH\Lib\C;

function every<T>(
  Traversable<T> $traversable,
  ?(function(T): bool) $predicate = NULL,
): bool;

If no predicate is provided, it defaults to casting the element to bool. If the Traversable is empty, returns true.

If you're looking for C\all, this is it.

Time complexity: O(n) Space complexity: O(1)

Parameters

Returns

  • bool

Examples

$strings = vec["a", "b", "c", "d"];
$predicate_result_1 = C\every($strings, $x ==> $x != "z");
echo "First predicate: $predicate_result_1\n";
//Output: First predicate: true
$predicate_result_2 = C\every($strings, $x ==> $x == "a");
echo "Second predicate: $predicate_result_2\n";
//Output: Second predicate: false
$predicate_result_3 = C\every($strings);
echo "Third predicate: $predicate_result_3\n";
//Output: Third predicate: true
$empty_strings = vec[];
$predicate_result_4 = C\every($empty_strings, $x ==> $x == "a");
echo "Fourth predicate: $predicate_result_4\n";
//Output: Fourth predicate: true
$predicate_result_5 = C\every($empty_strings);
echo "Fifth predicate: $predicate_result_5\n";
//Output: Fifth predicate: true