max method

E max({
  1. num value(
    1. dynamic
    )?,
})

Returns the element with the maximum value in the iterable.

An optional value function can be provided to define what is compared. If omitted, the element itself is used for comparison.

** Finding the longest string:**

final strings = ['a', 'cccc', 'bb'];
final result = strings.max(value: (s) => s.length); // 'cccc'

Implementation

E max({num Function(dynamic)? value}) {
  value ??= identity;
  E maxElement = this.first;
  num maxValue = value(maxElement);

  for (var element in skip(1)) {
    num current = value(element);
    if (current > maxValue) maxElement = element;
  }

  return maxElement;
}