-
Notifications
You must be signed in to change notification settings - Fork 7
/
number.ts
318 lines (295 loc) · 6.9 KB
/
number.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* The number module contains combinators for working with numbers.
*
* @module number
* @since 2.0.0
*/
import type { Ordering, Sortable } from "./sortable.ts";
import type { Combinable } from "./combinable.ts";
import type { Comparable } from "./comparable.ts";
import type { Initializable } from "./initializable.ts";
import type { Showable } from "./showable.ts";
import * as O from "./sortable.ts";
/**
* Compare two numbers and return true if they are equal.
*
* @example
* ```ts
* import * as N from "./number.ts";
*
* const result1 = N.compare(1)(2); // false
* const result2 = N.compare(1)(1); // true
* ```
*
* @since 2.0.0
*/
export function compare(second: number): (first: number) => boolean {
return (first) => first === second;
}
/**
* Compare two numbers and return true if first is less than or equal to second.
*
* @example
* ```ts
* import * as N from "./number.ts";
* import { pipe } from "./fn.ts";
*
* const result1 = pipe(1, N.lte(2)); // true
* const result2 = pipe(1, N.lte(1)); // true
* const result3 = pipe(2, N.lte(1)); // false
* ```
*
* @since 2.0.0
*/
export function lte(second: number): (first: number) => boolean {
return (first) => first <= second;
}
/**
* Multiply two numbers.
*
* @example
* ```ts
* import * as N from "./number.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(2, N.multiply(2)); // 4
* ```
*
* @since 2.0.0
*/
export function multiply(second: number): (first: number) => number {
return (first) => first * second;
}
/**
* Add two numbers.
*
* @example
* ```ts
* import * as N from "./number.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(2, N.add(2)); // 4
* ```
*
* @since 2.0.0
*/
export function add(second: number): (first: number) => number {
return (first) => first + second;
}
/**
* Return the positive modulus of two numbers.
*
* @example
* ```ts
* import * as N from "./number.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(11, N.mod(2)); // 1
* ```
*
* @since 2.0.0
*/
export function mod(second: number): (first: number) => number {
return (first) => ((first % second) + second) % second;
}
/**
* Return true if first evenly divides second.
*
* @example
* ```ts
* import * as N from "./number.ts";
* import { pipe } from "./fn.ts";
*
* const result1 = pipe(2, N.divides(3)); // false
* const result2 = pipe(2, N.divides(4)); // true
* ```
*
* @since 2.0.0
*/
export function divides(second: number): (first: number) => boolean {
return (first) => first <= second && mod(first)(second) === 0;
}
/**
* Compare two numbers and return their Ordering. 0 denotes equality, -1 denotes
* that first is less than second, and 1 denotes that first is greater than
* second.
*
* @example
* ```ts
* import * as N from "./number.ts";
* import { pipe } from "./fn.ts";
*
* const result1 = N.compare(1)(1); // 0
* const result2 = N.compare(2)(1); // 1
* const result3 = N.compare(1)(2); // -1
* ```
*
* @since 2.0.0
*/
export function sort(first: number, second: number): Ordering {
return O.sign(first - second);
}
/**
* A constant function that always returns 0. This is the additive identity and
* is used as the init value for InitializableNumberSum.
*
* @example
* ```ts
* import * as N from "./number.ts";
*
* const result = N.initZero(); // 0
* ```
*
* @since 2.0.0
*/
export function initZero(): number {
return 0;
}
/**
* A constant function that always returns 1. This is the multiplicative identity and
* is used as the init value for InitializableNumberProduct.
*
* @example
* ```ts
* import * as N from "./number.ts";
*
* const result = N.initOne(); // 1
* ```
*
* @since 2.0.0
*/
export function initOne(): number {
return 1;
}
/**
* A constant function that always returns Number.POSITIVE_INFINITY.
* This is the minimum identity and is used as the init value for
* InitializableNumberMinimum.
*
* @example
* ```ts
* import * as N from "./number.ts";
*
* const result = N.initPosInf(); // Number.POSITIVE_INFINITY
* ```
*
* @since 2.0.0
*/
export function initPosInf(): number {
return Number.POSITIVE_INFINITY;
}
/**
* A constant function that always returns Number.NEGATIVE_INFINITY.
* This is the maximum identity and is used as the init value for
* InitializableNumberMiximum.
*
* @example
* ```ts
* import * as N from "./number.ts";
*
* const result = N.initNegInf(); // Number.NEGATIVE_INFINITY
* ```
*
* @since 2.0.0
*/
export function initNegInf(): number {
return Number.NEGATIVE_INFINITY;
}
/**
* The canonical implementation of Comparable for number. It contains
* the method equal.
*
* @since 2.0.0
*/
export const ComparableNumber: Comparable<number> = { compare };
/**
* The canonical implementation of Sortable for number. It contains
* the method compare.
*
* @since 2.0.0
*/
export const SortableNumber: Sortable<number> = O.fromSort(sort);
/**
* A Combinable instance for number that uses multiplication for combineenation.
* It contains the method combine.
*
* @since 2.0.0
*/
export const CombinableNumberProduct: Combinable<number> = {
combine: multiply,
};
/**
* A Combinable instance for number that uses addition for combineenation.
* It contains the method combine.
*
* @since 2.0.0
*/
export const CombinableNumberSum: Combinable<number> = {
combine: add,
};
/**
* A Combinable instance for number that uses Math.max for combineenation.
* It contains the method combine.
*
* @since 2.0.0
*/
export const CombinableNumberMax: Combinable<number> = {
combine: O.max(SortableNumber),
};
/**
* A Combinable instance for number that uses Math.min for combineenation.
* It contains the method combine.
*
* @since 2.0.0
*/
export const CombinableNumberMin: Combinable<number> = {
combine: O.min(SortableNumber),
};
/**
* A Initializable instance for number that uses multiplication for combineenation.
* It contains the method combine.
*
* @since 2.0.0
*/
export const InitializableNumberProduct: Initializable<number> = {
combine: multiply,
init: initOne,
};
/**
* A Initializable instance for number that uses addition for combineenation.
* It contains the method combine.
*
* @since 2.0.0
*/
export const InitializableNumberSum: Initializable<number> = {
combine: add,
init: initZero,
};
/**
* A Initializable instance for number that uses Math.max for combineenation.
* It contains the method combine.
*
* @since 2.0.0
*/
export const InitializableNumberMax: Initializable<number> = {
combine: O.max(SortableNumber),
init: initNegInf,
};
/**
* A Initializable instance for number that uses Math.min for combineenation.
* It contains the method combine.
*
* @since 2.0.0
*/
export const InitializableNumberMin: Initializable<number> = {
combine: O.min(SortableNumber),
init: initPosInf,
};
/**
* The canonical instance of Showable for number. It contains the method show.
*
* @since 2.0.0
*/
export const ShowableNumber: Showable<number> = {
show: String,
};