min<T> function

  1. @Possible({TypeError})
  2. @useResult
T min<T>(
  1. T a,
  2. T b, {
  3. Select<T, Comparable<Object>>? by,
})

Returns the lesser of a and b.

If by is given, the produced Comparable is used. Otherwise a and b must be Comparables.

This function is unstable, either a or b may be returned if both are equal. Unlike math.min, it works on all Comparables.

min(('a', 1), ('a', 2), by: (e) => e.$2); // ('a', 1)

min(1, 2); // 1

double.nan values

This function and math.min handles double.nan differently. This function returns the other argument while math.min returns double.nan.

min(1.0, double.nan); // 1.0

math.min(1.0, double.nan); // double.nan

Implementation

@Possible({TypeError})
@useResult T min<T>(T a, T b, {Select<T, Comparable<Object>>? by}) => switch (by) {
  _? => by(a).compareTo(by(b)) < 0 ? a : b,
  _ => (a as Comparable).compareTo(b) < 0 ? a : b,
};