Fusonic\Linq\Linq::sum PHP Method

sum() public method

Gets the sum of all items or by invoking a transform function on each item to get a numeric value.
public sum ( callable $func = null ) : double
$func callable A func that returns any numeric type (int, float etc.) from the given element, or NULL to use the element itself.
return double The sum of all items.
    public function sum(callable $func = null)
    {
        $sum = 0;
        $iterator = $this->getSelectIteratorOrInnerIterator($func);
        foreach ($iterator as $value) {
            if (!is_numeric($value)) {
                throw new UnexpectedValueException("sum() only works on numeric values.");
            }
            $sum += $value;
        }
        return $sum;
    }