std/order
The result of comparing two values, and functions to work with it.
Compare functions in std have the type (a, a) -> Order, for example
int.compare and string.compare. list.sort takes such a function.
Order
The result of a comparison: the first value is Less than, Equal to,
or Greater than the second.
Order = Less | Equal | Greater negate
Turns Less into Greater and Greater into Less. Equal stays.
order.negate(Less) // Greater negate = (order : Order) -> Order { reverse
Gives a compare function that orders values the other way round.
list.sort([1, 3, 2], order.reverse(int.compare)) // [3, 2, 1] reverse = <A, N>(orderer : (A, A) <- N -> Order) -> (A, A) <- N -> Order { to_int
-1 for Less, 0 for Equal, and 1 for Greater.
to_int = (order : Order) -> Int { compare
Compares two orders, with Less < Equal < Greater.
compare = (a : Order, with : Order) -> Order { max
The greater of two orders, with Less < Equal < Greater.
max = (a : Order, b : Order) -> Order { min
The smaller of two orders, with Less < Equal < Greater.
min = (a : Order, b : Order) -> Order { break_tie
Gives with when order is Equal, and order when not. It puts
comparisons together: compare by the first key, and break ties by the
second.
order.break_tie(int.compare(1, 1), Less) // Less break_tie = (in : Order, with : Order) -> Order { lazy_break_tie
Like break_tie, but with is only called when it is needed.
lazy_break_tie = <N>(in : Order, with : () <- N -> Order) -> Order { is_less
True for Less.
is_less = (order : Order) -> Bool { order == Less } is_equal
True for Equal.
is_equal = (order : Order) -> Bool { order == Equal } is_greater
True for Greater.
is_greater = (order : Order) -> Bool { order == Greater }