YaLinqo\Enumerable::max PHP Method

max() public method

Syntax: max ()

Returns the maximum value in a sequence of values.

Syntax: max (selector {(v, k) ==> value})

Invokes a transform function on each element of a sequence and returns the maximum value.

public max ( callable | null $selector = null ) : number
$selector callable | null {(v, k) ==> value} A transform function to apply to each element. Default: value.
return number The maximum value in the sequence.
    public function max($selector = null)
    {
        $selector = Utils::createLambda($selector, 'v,k', Functions::$value);
        $max = -PHP_INT_MAX;
        $assigned = false;
        foreach ($this as $k => $v) {
            $max = max($max, $selector($v, $k));
            $assigned = true;
        }
        if (!$assigned) {
            throw new \UnexpectedValueException(Errors::NO_ELEMENTS);
        }
        return $max;
    }