YaLinqo\Enumerable::count PHP Method

count() public method

Syntax: count ()

If source iterator implements {@link Countable}, that implementation is used to obtain the count of elements. Otherwise, this method determines the count.

Syntax: count (predicate {(v, k) ==> result})

Returns a number that represents how many elements in the specified sequence satisfy a condition.

public count ( callable | null $predicate = null ) : integer
$predicate callable | null {(v, k) ==> result} A function to test each element for a condition. Default: null.
return integer The number of elements in the input sequence.
    public function count($predicate = null)
    {
        $it = $this->getIterator();
        if ($it instanceof \Countable && $predicate === null) {
            return count($it);
        }
        $predicate = Utils::createLambda($predicate, 'v,k', Functions::$value);
        $count = 0;
        foreach ($it as $k => $v) {
            if ($predicate($v, $k)) {
                $count++;
            }
        }
        return $count;
    }