-
Notifications
You must be signed in to change notification settings - Fork 7
/
these.ts
320 lines (289 loc) · 6.78 KB
/
these.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
/**
* This file contains the These algebraic data type. These is a sort of
* combination of Either and Pair. It can represent a complete failure or a
* partial failure.
*
* @module These
* @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 { Flatmappable } from "./flatmappable.ts";
import type { Initializable } from "./initializable.ts";
import type { Mappable } from "./mappable.ts";
import type { Foldable } from "./foldable.ts";
import type { Showable } from "./showable.ts";
import type { Traversable } from "./traversable.ts";
import * as E from "./either.ts";
import { pipe } from "./fn.ts";
/**
* @since 2.0.0
*/
export type Left<B> = E.Left<B>;
/**
* @since 2.0.0
*/
export type Right<A> = E.Right<A>;
/**
* @since 2.0.0
*/
export type Both<B, A> = { tag: "Both"; left: B; right: A };
/**
* @since 2.0.0
*/
export type These<B, A> = Left<B> | Right<A> | Both<B, A>;
/**
* @since 2.0.0
*/
export interface KindThese extends Kind {
readonly kind: These<Out<this, 1>, Out<this, 0>>;
}
/**
* @since 2.0.0
*/
export interface KindRightThese<E> extends Kind {
readonly kind: These<E, Out<this, 0>>;
}
/**
* @since 2.0.0
*/
export function left<A = never, B = never>(left: B): These<B, A> {
return ({ tag: "Left", left });
}
/**
* @since 2.0.0
*/
export function right<A = never, B = never>(right: A): These<B, A> {
return ({ tag: "Right", right });
}
/**
* @since 2.0.0
*/
export function both<A, B>(left: B, right: A): These<B, A> {
return ({ tag: "Both", left, right });
}
/**
* @since 2.0.0
*/
export function match<A, B, O>(
onLeft: (b: B) => O,
onRight: (a: A) => O,
onBoth: (b: B, a: A) => O,
): (ta: These<B, A>) => O {
const _fold = E.match(onLeft, onRight);
return (ta) => ta.tag === "Both" ? onBoth(ta.left, ta.right) : _fold(ta);
}
/**
* @since 2.0.0
*/
export function isLeft<A, B>(ta: These<B, A>): ta is Left<B> {
return ta.tag === "Left";
}
/**
* @since 2.0.0
*/
export function isRight<A, B>(ta: These<B, A>): ta is Right<A> {
return ta.tag === "Right";
}
/**
* @since 2.0.0
*/
export function isBoth<A, B>(ta: These<B, A>): ta is Both<B, A> {
return ta.tag === "Both";
}
/**
* @since 2.0.0
*/
export function wrap<A, B = never>(a: A): These<B, A> {
return right(a);
}
/**
* @since 2.0.0
*/
export function fail<A = never, B = never>(b: B): These<B, A> {
return left(b);
}
/**
* @since 2.0.0
*/
export function map<A, I>(
fai: (a: A) => I,
): <B>(ta: These<B, A>) => These<B, I> {
return match(
left,
(value) => right(fai(value)),
(left, right) => both(left, fai(right)),
);
}
/**
* @since 2.0.0
*/
export function mapSecond<B, J>(
fbj: (b: B) => J,
): <A>(ta: These<B, A>) => These<J, A> {
return match(
(value) => left(fbj(value)),
right,
(left, right) => both(fbj(left), right),
);
}
/**
* @since 2.0.0
*/
export function fold<A, O>(
foao: (o: O, a: A) => O,
o: O,
): <B>(ta: These<B, A>) => O {
return (ta) => isLeft(ta) ? o : foao(o, ta.right);
}
/**
* @since 2.0.0
*/
export function traverse<U extends Kind>(
A: Applicable<U>,
): <A, I, J, K, L, M>(
favi: (a: A) => $<U, [I, J, K], [L], [M]>,
) => <B>(ta: These<B, A>) => $<U, [These<B, I>, J, K], [L], [M]> {
return (favi) =>
match(
(b) => A.wrap(left(b)),
(a) => pipe(favi(a), A.map((i) => right(i))),
(b, a) => pipe(favi(a), A.map((i) => both(b, i))),
);
}
/**
* @since 2.0.0
*/
export function getShowableThese<A, B>(
SB: Showable<B>,
SA: Showable<A>,
): Showable<These<B, A>> {
return ({
show: match(
(left) => `Left(${SB.show(left)})`,
(right) => `Right(${SA.show(right)})`,
(left, right) => `Both(${SB.show(left)}, ${SA.show(right)})`,
),
});
}
/**
* @since 2.0.0
*/
export function getCombinableThese<A, B>(
SA: Combinable<A>,
SB: Combinable<B>,
): Combinable<These<B, A>> {
return ({
combine: (x) => (y) => {
if (isLeft(x)) {
if (isLeft(y)) {
return left(SB.combine(x.left)(y.left));
} else if (isRight(y)) {
return both(x.left, y.right);
}
return both(SB.combine(x.left)(y.left), y.right);
}
if (isRight(x)) {
if (isLeft(y)) {
return both(y.left, x.right);
} else if (isRight(y)) {
return right(SA.combine(x.right)(y.right));
}
return both(y.left, SA.combine(x.right)(y.right));
}
if (isLeft(y)) {
return both(SB.combine(x.left)(y.left), x.right);
} else if (isRight(y)) {
return both(x.left, SA.combine(x.right)(y.right));
}
return both(SB.combine(x.left)(y.left), SA.combine(x.right)(y.right));
},
});
}
/**
* @since 2.0.0
*/
export function getInitializableThese<A, B>(
IA: Initializable<A>,
IB: Initializable<B>,
): Initializable<These<B, A>> {
return {
init: () => both(IB.init(), IA.init()),
...getCombinableThese(IA, IB),
};
}
/**
* @since 2.0.0
*/
export function getFlatmappableRight<B>(
{ combine }: Combinable<B>,
): Flatmappable<KindRightThese<B>> {
const Flatmappable: Flatmappable<KindRightThese<B>> = {
wrap,
map,
apply: (ua) => (ufai) => {
if (isRight(ua)) {
if (isRight(ufai)) {
return right(ufai.right(ua.right));
} else if (isLeft(ufai)) {
return ufai;
} else {
return both(ufai.left, ufai.right(ua.right));
}
} else if (isLeft(ua)) {
if (isRight(ufai)) {
return ua;
} else {
return left(combine(ua.left)(ufai.left));
}
} else {
if (isRight(ufai)) {
return both(ua.left, ufai.right(ua.right));
} else if (isLeft(ufai)) {
return left(combine(ua.left)(ufai.left));
} else {
return both(combine(ua.left)(ufai.left), ufai.right(ua.right));
}
}
},
flatmap: (faui) => (ua) => {
if (isLeft(ua)) {
return ua;
} else if (isRight(ua)) {
return faui(ua.right);
} else {
const result = faui(ua.right);
if (isLeft(result)) {
return left(combine(result.left)(ua.left));
} else if (isRight(result)) {
return both(ua.left, result.right);
} else {
return both(combine(result.left)(ua.left), result.right);
}
}
},
};
return Flatmappable;
}
/**
* @since 2.0.0
*/
export const BimappableThese: Bimappable<KindThese> = { map, mapSecond };
/**
* @since 2.0.0
*/
export const MappableThese: Mappable<KindThese> = { map };
/**
* @since 2.0.0
*/
export const FoldableThese: Foldable<KindThese> = { fold };
/**
* @since 2.0.0
*/
export const TraversableThese: Traversable<KindThese> = {
map,
fold,
traverse,
};