Std.Comparable (Cure Standard Library v0.37.2)

View Source

Total ordering: the Comparable(t) interface, its Int and Float instances, and what derives from < alone: <=, >, >=, compare, min, max and clamp.

The sole primitive obligation is `<`; `<=`, `>`, `>=` and the three-way compare (returning an Ordering) are derived requires Comparable(t) top-level functions, each expressed with `<` alone (a total order is trichotomous in `<`). Comparable(t) requires Equatable(t), so every comparable type is also equatable.

The derived operations are standalone requires-constrained functions rather than interface defaults: an instance-specialised default re-dispatching a sibling method at a concrete operand cannot be elaborated for every leaf today, whereas a requires-constrained function stays polymorphic and is resolved per call site (the same idiom Std.Equatable. `!=` uses).

Examples

use Std.Comparable

compare(1, 2)                           # => LessThan
min(3, 7)                               # => 3
clamp(15, 0, 10)                        # => 10

Summary

Types

Group tag consumed by Cure.Stdlib.Preload. Three-way ordering result.

Callbacks

Total ordering over t. Requires Equatable(t) so every comparable type is equatable; the ordering itself is generated from `<` alone.

Functions

Strict less-than. Sole primitive obligation of the class.

`<=` derived: a <= b iff not (b < a).

`>` derived by flipping the operands: a > b iff b < a.

`>=` derived: a >= b iff not (a < b).

Clamp value into the inclusive range [lo, hi].

Derived tripartite compare, generated from `<` alone (trichotomy of a total order). Kept present and named compare so compare_op_call and min/max/clamp (which dispatch to compare) keep working.

Pointwise maximum under the Comparable ordering.

Pointwise minimum under the Comparable ordering.

Types

enum Ordering

 enum Ordering
   LessThan
   EqualTo
   GreaterThan

Group tag consumed by Cure.Stdlib.Preload. Three-way ordering result.

Callbacks

interface Comparable(t)

 interface Comparable(t)

Total ordering over t. Requires Equatable(t) so every comparable type is equatable; the ordering itself is generated from `<` alone.

Functions

<(a: t, b: t) -> Bool

 fn <(a: t, b: t) -> Bool

Strict less-than. Sole primitive obligation of the class.

<=(a: t, b: t) -> Bool

 fn <=(a: t, b: t) -> Bool

`<=` derived: a <= b iff not (b < a).

>(a: t, b: t) -> Bool

 fn >(a: t, b: t) -> Bool

`>` derived by flipping the operands: a > b iff b < a.

>=(a: t, b: t) -> Bool

 fn >=(a: t, b: t) -> Bool

`>=` derived: a >= b iff not (a < b).

clamp(value: t, lo: t, hi: t) -> t

 fn clamp(value: t, lo: t, hi: t) -> t

Clamp value into the inclusive range [lo, hi].

compare(a: t, b: t) -> Ordering

 fn compare(a: t, b: t) -> Ordering

Derived tripartite compare, generated from `<` alone (trichotomy of a total order). Kept present and named compare so compare_op_call and min/max/clamp (which dispatch to compare) keep working.

max(x: t, y: t) -> t

 fn max(x: t, y: t) -> t

Pointwise maximum under the Comparable ordering.

min(x: t, y: t) -> t

 fn min(x: t, y: t) -> t

Pointwise minimum under the Comparable ordering.