Fusonic\Linq\Linq::aggregate PHP Метод

aggregate() публичный Метод

The aggregate method makes it simple to perform a calculation over a sequence of values. This method works by calling $func one time for each element. The first element of source is used as the initial aggregate value if $seed parameter is not specified. If $seed is specified, this value will be used as the first value.
public aggregate ( callable $func, mixed $seed = null ) : mixed
$func callable An accumulator function to be invoked on each element.
$seed mixed
Результат mixed Returns the final result of $func.
    public function aggregate(callable $func, $seed = null)
    {
        $result = null;
        $first = true;
        if ($seed !== null) {
            $result = $seed;
            $first = false;
        }
        foreach ($this->iterator as $current) {
            if (!$first) {
                $result = $func($result, $current);
            } else {
                $result = $current;
                $first = false;
            }
        }
        if ($first) {
            throw new \RuntimeException("The input sequence contains no elements.");
        }
        return $result;
    }