本页尚未翻译,因此以英文显示。 参与翻译

gt · gte · lt · lte

Data-first comparisons, as function values instead of operators.

bool gt(Object? a, Object? b) // a > b bool gte(Object? a, Object? b) // a >= b bool lt(Object? a, Object? b) // a < b bool lte(Object? a, Object? b) // a <= b

Lecture

gt, gte, lt, and lte are the four ordering operators packaged as data-first functions: gt(a, b) == (a > b), with the first argument on the left, exactly like the operator. That makes them drop-in comparators wherever an API wants a function instead of an infix operator.

Under the hood they require both values to be mutually Comparable and — with one exception — the exact same runtime type: num vs num and String vs String are allowed to mix (so int can compare against double), but anything else with mismatched types throws an ArgumentError rather than silently coercing.

None of the four are curried — there's no gt(5) partial application. For a reusable unary predicate (say, for filter), write a small closure that fixes one side: (b) => gt(b, 5).

Demo 1 · Basics, and the type-mismatch error

Demo 2 · Currying into a filter predicate

Try it yourself

Exercise: keep only the ages that are gte 18 (adults).

Related: add — the arithmetic counterpart to these comparisons · sort / sortBy — build a comparator from lt/gt · min / max — aggregate versions of these comparisons · negate — flip a curried comparison predicate