YaLinqo\Enumerable::sum PHP Method

sum() public method

Syntax: sum ()

Computes the sum of a sequence of values.

Syntax: sum (selector {(v, k) ==> result})

Computes the sum of the sequence of values that are obtained by invoking a transform function on each element of the input sequence.

This method returns zero if source contains no elements.

public sum ( callable | null $selector = null ) : number
$selector callable | null {(v, k) ==> result} A transform function to apply to each element.
return number The sum of the values in the sequence.
    public function sum($selector = null)
    {
        $selector = Utils::createLambda($selector, 'v,k', Functions::$value);
        $sum = 0;
        foreach ($this as $k => $v) {
            $sum += $selector($v, $k);
        }
        return $sum;
    }