-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathOrdering.ts
122 lines (105 loc) · 2.87 KB
/
Ordering.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @since 2.0.0
*/
import * as E from './Eq'
import * as M from './Monoid'
import * as S from './Semigroup'
// -------------------------------------------------------------------------------------
// model
// -------------------------------------------------------------------------------------
/**
* @category model
* @since 2.0.0
*/
export type Ordering = -1 | 0 | 1
/**
* Less strict version of [`match`](#match).
*
* The `W` suffix (short for **W**idening) means that the handler return types will be merged.
*
* @category pattern matching
* @since 2.12.0
*/
export const matchW =
<A, B, C>(onLessThan: () => A, onEqual: () => B, onGreaterThan: () => C) =>
(o: Ordering): A | B | C =>
o === -1 ? onLessThan() : o === 0 ? onEqual() : onGreaterThan()
/**
* @category pattern matching
* @since 2.10.0
*/
export const match: <A>(onLessThan: () => A, onEqual: () => A, onGreaterThan: () => A) => (o: Ordering) => A = matchW
// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
/**
* @since 2.10.0
*/
export const reverse = (o: Ordering): Ordering => (o === -1 ? 1 : o === 1 ? -1 : 0)
// -------------------------------------------------------------------------------------
// instances
// -------------------------------------------------------------------------------------
/**
* @category instances
* @since 2.10.0
*/
export const Eq: E.Eq<Ordering> = {
equals: (x, y) => x === y
}
/**
* @category instances
* @since 2.10.0
*/
export const Semigroup: S.Semigroup<Ordering> = {
concat: (x, y) => (x !== 0 ? x : y)
}
/**
* @category instances
* @since 2.10.0
*/
export const Monoid: M.Monoid<Ordering> = {
concat: Semigroup.concat,
empty: 0
}
// -------------------------------------------------------------------------------------
// utils
// -------------------------------------------------------------------------------------
/**
* @since 2.0.0
*/
export const sign = (n: number): Ordering => (n <= -1 ? -1 : n >= 1 ? 1 : 0)
// -------------------------------------------------------------------------------------
// deprecated
// -------------------------------------------------------------------------------------
/**
* Use [`reverse`](#reverse) instead.
*
* @category zone of death
* @since 2.0.0
* @deprecated
*/
export const invert = reverse
/**
* Use [`Semigroup`](#semigroup) instead
*
* @category zone of death
* @since 2.0.0
* @deprecated
*/
export const semigroupOrdering: S.Semigroup<Ordering> = Semigroup
/**
* Use [`Eq`](#eq) instead
*
* @category zone of death
* @since 2.0.0
* @deprecated
*/
export const eqOrdering: E.Eq<Ordering> = Eq
/**
* Use [`Monoid`](#monoid) instead
*
* @category zone of death
* @since 2.4.0
* @deprecated
*/
export const monoidOrdering: M.Monoid<Ordering> = Monoid