Arrays\filterCount()

higher-order reducer terminal accepts iterable returns Closure pure
(T → bool) → (Iterable → int)
At a glance — Terminal — consumes the whole source and returns an int. Equivalent to count(filter(fn)($src)) in one step.

Creates a Closure that counts how many elements of an array or iterable pass the predicate.

/**
  * @param callable(mixed):bool $function
  * @return Closure(iterable<int|string, mixed>):int
  */
Arrays\filterCount(callable $function): Closure

Returned Closure

When Arrays\filterCount() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param iterable<int|string, mixed> $source
  * @return int
  */
$function (iterable $source): int

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

$evenCount = Arrays\filterCount(fn($v) => $v % 2 === 0);
echo $evenCount([1, 2, 3, 4, 5, 6]); // 3

Curried

This can be called inline using currying.

echo Arrays\filterCount('is_string')([1, 'a', 2, 'b', null]); // 2

Details

Arrays Functions

Releated Array filter Functions