minBy<R extends Comparable> method

T? minBy<R extends Comparable>(
  1. R selector(
    1. T
    )
)

Returns the first element yielding the smallest value of the given function or null if there are no elements.

Implementation

T? minBy<R extends Comparable>(R Function(T) selector) {
  final i = iterator();
  if (!iterator().hasNext()) return null;
  T minElement = i.next();
  R minValue = selector(minElement);
  while (i.hasNext()) {
    final e = i.next();
    final v = selector(e);
    if (minValue.compareTo(v) > 0) {
      minElement = e;
      minValue = v;
    }
  }
  return minElement;
}