-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathord.go
67 lines (55 loc) · 1.39 KB
/
ord.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/golem
//
package ord
import "github.com/fogfish/golem/pure"
// Ordering type defines enum{ LT, EQ, GT }
type Ordering int
// enum{ LT, EQ, GT }
const (
LT Ordering = -1
EQ Ordering = 0
GT Ordering = 1
)
// Ord : T ⟼ T ⟼ Ordering
// Each type implements compare rules, mapping pair of value to enum{ LT, EQ, GT }
type Ord[T any] interface {
Compare(T, T) Ordering
}
// ord generic implementation for built-in types
type ord[T pure.AnyOrderable] string
func (ord[T]) Compare(a, b T) Ordering {
switch {
case a < b:
return LT
case a > b:
return GT
default:
return EQ
}
}
const (
Int = ord[int]("eq.int")
String = ord[string]("eq.string")
)
// From is a combinator that lifts T ⟼ T ⟼ Ordering function to
// an instance of Ord type trait
type From[T any] func(T, T) Ordering
func (f From[T]) Compare(a, b T) Ordering { return f(a, b) }
// ContraMap is a combinator that build a new instance of type trait Ord[B] using
// existing instance of Ord[A] and f: b ⟼ a
type ContraMap[A, B any] struct {
Ord[A]
pure.ContraMap[A, B]
}
// Equal implementation of contra variant functor
func (f ContraMap[A, B]) Compare(a, b B) Ordering {
return f.Ord.Compare(
f.ContraMap(a),
f.ContraMap(b),
)
}