aboutsummaryrefslogtreecommitdiff
path: root/std/prelude/compare.pk
blob: 5f3ae744a186711825cb2a73fd7f1a13d36267b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## std.compare: Classes for comparable types.
## reference: https://doc.rust-lang.org/std/cmp

## The Eq class. For types with some notion of equivalence.
pub type Eq = class
  ==(Self, Self): bool

## A blanket implementation of a corresponding not-equal function.
pub !=[T: Eq](a: T, b: T): bool =
  not(a == b)

## The Compare class. For a type comparable with itself.
pub type Compare = class
  <(a: Self, b: Self): bool

## A blanket implementation of a corresponding greater-than function.
## Note to self: do NOT inline!
pub func >[T: Compare](a: T, b: T): bool =
  b < a

## The Ord class. For types with some notion of equivalence and comparision.
##
## Note: This is *not* a mathematical notion of an order!
## No invariants on `<` nor `==` are guaranteed to hold, as classes
## are implicitly implementable.
pub type Ord = class
  <(a: Self, b: Self): bool
  ==(a: Self, b: Self): bool

## A blanket implementation of a corresponding less-than-or-equal function.
pub func <=[T: Ord](a: T, b: T): bool =
  a < b or a == b

## A blanket implementation of a corresponding greater-than-or-equal function.
pub func >=[T: Ord](a: T, b: T): bool =
  a > b or a == b