-
Notifications
You must be signed in to change notification settings - Fork 7
/
pair.ts
540 lines (510 loc) · 11.6 KB
/
pair.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/**
* Pair represents a pair of values. It can be thought of as a tuple
* of two, or first and second, or separated values.
*
* @module Pair
* @since 2.0.0
*/
import type { $, Kind, Out } from "./kind.ts";
import type { Applicable } from "./applicable.ts";
import type { Bimappable } from "./bimappable.ts";
import type { Combinable } from "./combinable.ts";
import type { Comparable } from "./comparable.ts";
import type { Flatmappable } from "./flatmappable.ts";
import type { Foldable } from "./foldable.ts";
import type { Initializable } from "./initializable.ts";
import type { Mappable } from "./mappable.ts";
import type { Showable } from "./showable.ts";
import type { Sortable } from "./sortable.ts";
import type { Traversable } from "./traversable.ts";
import { createFlatmappable } from "./flatmappable.ts";
import { dual } from "./combinable.ts";
import { pipe } from "./fn.ts";
/**
* Pair represents a pair of values. This is
* equivalent to a Tuple of length two, the
* Separated type in fp-ts, and any other type
* that contains exactly two covariant other
* types.
*
* The primary use fo Pair in this library
* is the target of a partition, where some
* type A is partitioned, either into
* [A, A], or [A, B] where B extends A.
*
* Other uses will likely come when Arrows
* are implemented in fun.
*
* @since 2.0.0
*/
export type Pair<A, B> = readonly [A, B];
/**
* Specifies Pair as a Higher Kinded Type, with covariant
* parameters A and B corresponding to the 0th and 1st
* index of any Substitutions.
*
* @since 2.0.0
*/
export interface KindPair extends Kind {
readonly kind: Pair<Out<this, 0>, Out<this, 1>>;
}
/**
* Creates a Pair from two values first and second with types
* A and B respectively. Used to quickly construct a Pair.
*
* @example
* ```ts
* import * as P from "./pair.ts";
*
* const nameAndAge = P.pair("Brandon", 37);
*
* const name = P.getFirst(nameAndAge); // "Brandon"
* const age = P.getSecond(nameAndAge); // 37
* ```
*
* @since 2.0.0
*/
export function pair<A, B>(first: A, second: B): Pair<A, B> {
return [first, second];
}
/**
* Creates a pair from a single type
*
* @example
* ```ts
* import { dup } from "./pair.ts";
*
* const result = dup(1); // [1, 1]
* ```
*
* @since 2.0.0
*/
export function dup<A>(a: A): Pair<A, A> {
return pair(a, a);
}
/**
* Apply a function in the first position of a pair to a value
* in the second position of a pair.
*
* @example
* ```ts
* import * as P from "./pair.ts";
* import { flow } from "./fn.ts";
*
* const double = flow(
* P.dup<number>,
* P.map(n => (m: number) => n + m),
* P.merge,
* );
*
* const result1 = double(1); // 2
* const result2 = double(2); // 4
* ```
*
* @since 2.0.0
*/
export function merge<A, I>(ua: Pair<(a: A) => I, A>): I {
return ua[0](ua[1]);
}
/**
* Apply a function in the first position of a pair to a value
* in the second position of a pair.
*
* @example
* ```ts
* import * as P from "./pair.ts";
* import { flow } from "./fn.ts";
*
* const double = flow(
* P.dup<number>,
* P.mapSecond(n => (m: number) => n + m),
* P.mergeSecond,
* );
*
* const result1 = double(1); // 2
* const result2 = double(2); // 4
* ```
*
* @since 2.0.0
*/
export function mergeSecond<A, I>(ua: Pair<A, (a: A) => I>): I {
return ua[1](ua[0]);
}
/**
* Extracts the first value from a Pair.
*
* @example
* ```ts
* import * as P from "./pair.ts";
* import { pipe } from "./fn.ts";
*
* const shouldBe1 = pipe(
* P.pair(1, 2),
* P.getFirst
* ); // 1
* ```
*
* @since 2.0.0
*/
export function getFirst<A, B>([first]: Pair<A, B>): A {
return first;
}
/**
* Extracts the second value from a Pair.
*
* @example
* ```ts
* import * as P from "./pair.ts";
* import { pipe } from "./fn.ts";
*
* const shouldBe2 = pipe(
* P.pair(1, 2),
* P.getSecond
* ); // 2
* ```
*
* @since 2.0.0
*/
export function getSecond<A, B>([_, second]: Pair<A, B>): B {
return second;
}
/**
* A curried form of the pair constructor, starting with the first
* value of a pair.
*
* @example
* ```ts
* import * as P from "./pair.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* 37,
* P.first("Brandon"),
* P.mapSecond(n => n + 1),
* ); // ["Brandon", 38]
* ```
*
* @since 2.0.0
*/
export function first<A>(first: A): <B>(second: B) => Pair<A, B> {
return (second) => pair(first, second);
}
/**
* A curried form of the pair constructor, starting with the second
* value of a pair.
*
* @example
* ```ts
* import * as P from "./pair.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* 37,
* P.second("Brandon"),
* P.map(n => n + 1),
* ); // [38, "Brandon"]
* ```
*
* @since 2.0.0
*/
export function second<B>(second: B): <A>(first: A) => Pair<A, B> {
return (first) => pair(first, second);
}
/**
* Creates a new Pair with the first and second values swapped.
*
* @example
* ```ts
* import * as P from "./pair.ts";
* import { pipe } from "./fn.ts";
*
* const shouldBe2 = pipe(
* P.pair(1, 2),
* P.swap,
* P.first
* ); // 2
* ```
*
* @since 2.0.0
*/
export function swap<A, B>([first, second]: Pair<A, B>): Pair<B, A> {
return pair(second, first);
}
/**
* Creates a new Pair with the same second value and a new first
* value determined by the output of the fai function.
*
* @example
* ```ts
* import * as P from "./pair.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* P.pair(1, 2),
* P.map(String),
* ); // ['1', 2]
* ```
*
* @since 2.0.0
*/
export function map<A, I>(
fai: (a: A) => I,
): <B>(ta: Pair<A, B>) => Pair<I, B> {
return ([first, second]) => pair(fai(first), second);
}
/**
* Creates a new Pair with the same first value and a new second
* value determined by the output of the fbj function.
*
* @example
* ```ts
* import * as P from "./pair.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* P.pair(1, 2),
* P.mapSecond(String),
* ); // [1, '2']
* ```
*
* @since 2.0.0
*/
export function mapSecond<B, J>(
fbj: (a: B) => J,
): <A>(ta: Pair<A, B>) => Pair<A, J> {
return ([first, second]) => pair(first, fbj(second));
}
/**
* Creates a new pair by mapping first through the fai
* function and second through the fbj function.
*
* @example
* ```ts
* import * as P from "./pair.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* P.pair(1, 2),
* P.bimap(String, n => n + 1),
* ); // ['1', 3]
* ```
*
* @since 2.0.0
*/
export function bimap<A, B, I, J>(
fbj: (b: B) => J,
fai: (a: A) => I,
): (ta: Pair<A, B>) => Pair<I, J> {
return ([first, second]) => pair(fai(first), fbj(second));
}
/**
* Just like the first function, unwrap returns the first
* value in a pair.
*
* @example
* ```ts
* import { pair, unwrap } from "./pair.ts";
*
* const result = unwrap(pair(1, 2)); // 1
* ```
*
* @since 2.0.0
*/
export function unwrap<A, B>([first]: Pair<A, B>): A {
return first;
}
/**
* Reduces a pair with an initial value, also passing
* the second value into the foldr as well.
*
* @example
* ```ts
* import { pair, fold } from "./pair.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* pair(10, 20),
* fold(Math.max, Number.NEGATIVE_INFINITY),
* ); // 20
* ```
*
* @since 2.0.0
*/
export function fold<A, B, O>(
foao: (acc: O, first: A, second: B) => O,
initial: O,
): (ua: Pair<A, B>) => O {
return ([first, second]) => foao(initial, first, second);
}
/**
* Traverse a pair using another algebraic structure's Applicable.
*
* @example
* ```ts
* import { traverse, pair } from "./pair.ts";
* import { some, ApplicableOption, fromPredicate } from "./option.ts";
* import { pipe } from "./fn.ts";
*
* const traverseOption = traverse(ApplicableOption);
* const startsWithB = fromPredicate(
* (name: string) => name.startsWith("B")
* );
*
* const result1 = pipe(
* pair("Brandon", 37),
* traverseOption(startsWithB),
* ); // { tag: "Some", value: ["Brandon", 37] }
*
* const result2 = pipe(
* pair("Alice", 37),
* traverseOption(startsWithB),
* ); // { tag: "None" }
* ```
*
* @since 2.0.0
*/
export function traverse<V extends Kind>(A: Applicable<V>): <A, I, J, K, L, M>(
favi: (a: A) => $<V, [I, J, K], [L], [M]>,
) => <B>(ua: Pair<A, B>) => $<V, [Pair<I, B>, J, K], [L], [M]> {
return <A, I, J, K, L, M>(
favi: (a: A) => $<V, [I, J, K], [L], [M]>,
): <B>(ua: Pair<A, B>) => $<V, [Pair<I, B>, J, K], [L], [M]> =>
([fst, snd]) =>
pipe(
favi(fst),
A.map(second(snd)),
);
}
/**
* Creates a Showable instance for a pair, wrapping the Showable instances provided
* for the first and second values.
*
* @since 2.0.0
*/
export function getShowablePair<A, B>(
SA: Showable<A>,
SB: Showable<B>,
): Showable<Pair<A, B>> {
return {
show: ([first, second]) => `Pair(${SA.show(first)}, ${SB.show(second)})`,
};
}
/**
* @since 2.0.0
*/
export function getCombinablePair<A, B>(
CA: Combinable<A>,
CB: Combinable<B>,
): Combinable<Pair<A, B>> {
return {
combine: (second) => (first) => [
CA.combine(second[0])(first[0]),
CB.combine(second[1])(first[1]),
],
};
}
/**
* @since 2.0.0
*/
export function getInitializablePair<A, B>(
IA: Initializable<A>,
IB: Initializable<B>,
): Initializable<Pair<A, B>> {
return {
init: () => [IA.init(), IB.init()],
...getCombinablePair(IA, IB),
};
}
/**
* @since 2.0.0
*/
export function getComparablePair<A, B>(
CA: Comparable<A>,
CB: Comparable<B>,
): Comparable<Pair<A, B>> {
return {
compare: (second) => (first) =>
CA.compare(second[0])(first[0]) && CB.compare(second[1])(first[1]),
};
}
/**
* @since 2.0.0
*/
export function getSortablePair<A, B>(
SA: Sortable<A>,
SB: Sortable<B>,
): Sortable<Pair<A, B>> {
return ({
sort: (first, second) => {
const oa = SA.sort(first[0], second[0]);
return oa === 0 ? SB.sort(first[1], second[1]) : oa;
},
});
}
/**
* A Kind implementation used to fix the second parameter in a Pair.
* Otherwise it operates the same as Pair does.
*
* @since 2.0.0
*/
export interface KindRightPair<B> extends Kind {
readonly kind: Pair<Out<this, 0>, B>;
}
/**
* Creates a Flatmappable instance for Pair where the second parameter is
* concatenated according to the Monoid instance passed in.
*
* @example
* ```ts
* import { InitializableNumberSum } from "./number.ts";
* import { getRightFlatmappable, pair } from "./pair.ts";
* import { pipe } from "./fn.ts";
*
* const Flatmappable = getRightFlatmappable(InitializableNumberSum);
*
* const ageOneYear = (name: string) => pair(name, 1);
*
* const result = pipe(
* pair("Brandon", 36), // Pair(Name, Age)
* Flatmappable.flatmap(ageOneYear),
* Flatmappable.flatmap(ageOneYear)
* ); // ["Brandon", 38]
* ```
*
* @since 2.0.0
*/
export function getRightFlatmappable<L>(
I: Initializable<L>,
): Flatmappable<KindRightPair<L>> {
const { combine } = dual(I);
return createFlatmappable<KindRightPair<L>>({
wrap: (a) => pair(a, I.init()),
flatmap: (fati) => ([first, second]) =>
pipe(fati(first), mapSecond(combine(second))),
});
}
/**
* The canonical Mappable instance for Pair. Contains the
* map method.
*
* @since 2.0.0
*/
export const MappablePair: Mappable<KindPair> = { map };
/**
* The canonical Bimappable instance for Pair. Contains the
* bimap and mapSecond methods.
*
* @since 2.0.0
*/
export const BimappablePair: Bimappable<KindPair> = { mapSecond, map };
/**
* The canonical Foldable instance for Pair. Contains the
* fold method.
*
* @since 2.0.0
*/
export const FoldablePair: Foldable<KindPair> = { fold };
/**
* @since 2.0.0
*/
export const TraversablePair: Traversable<KindPair> = { map, fold, traverse };