max method

TValue max([
  1. dynamic selector(
    1. TValue
    )?
])

Finds the element with the largest value selected by the selector function.

All values returned by the selector must be of type num. If selector is null, the elements itself become the selected elements.

Implementation

TValue max([Function(TValue)? selector]) {
  selector ??= (e) => e;

  TValue? maxObject;
  num? maxValue;
  for (final element in this) {
    final value = selector(element);
    if (value == null) {
      throw BoostException('Selector for element $element returned null!');
    } else if (value is! num) {
      throw BoostException(
          'Selector for element $element did not return num!');
    }

    if (maxValue == null || value > maxValue) {
      maxObject = element;
      maxValue = value;
    }
  }

  if (maxObject == null) {
    throw BoostException('Iterable is empty.');
  }

  return maxObject;
}