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;
    }