aang 2.0.0-alpha.3 → 2.0.0-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/result.js ADDED
@@ -0,0 +1,439 @@
1
+ import { None, Some } from "./option.js";
2
+ import { Pair } from "./pair.js";
3
+ import { Task } from "./task.js";
4
+ class ResultTrait {
5
+ toString() {
6
+ return this.isOkay
7
+ ? `Okay(${String(this.value)})`
8
+ : `Fail(${String(this.value)})`;
9
+ }
10
+ fold(okayMorphism, failMorphism) {
11
+ return this.isOkay ? okayMorphism(this.value) : failMorphism(this.value);
12
+ }
13
+ map(okayMorphism, failMorphism) {
14
+ return this.isOkay
15
+ ? new Okay(okayMorphism(this.value))
16
+ : new Fail(failMorphism(this.value));
17
+ }
18
+ mapOkay(morphism) {
19
+ return this.isOkay ? new Okay(morphism(this.value)) : this;
20
+ }
21
+ mapFail(morphism) {
22
+ return this.isFail ? new Fail(morphism(this.value)) : this;
23
+ }
24
+ replace(okayValue, failValue) {
25
+ return this.isOkay ? new Okay(okayValue) : new Fail(failValue);
26
+ }
27
+ replaceOkay(value) {
28
+ return this.isOkay ? new Okay(value) : this;
29
+ }
30
+ replaceFail(value) {
31
+ return this.isFail ? new Fail(value) : this;
32
+ }
33
+ and(that) {
34
+ if (this.isFail)
35
+ return this;
36
+ if (that.isFail)
37
+ return that;
38
+ return new Okay(new Pair(this.value, that.value));
39
+ }
40
+ andThen(that) {
41
+ return this.isOkay ? that : this;
42
+ }
43
+ andWhen(that) {
44
+ return this.isOkay && that.isFail ? that : this;
45
+ }
46
+ or(that) {
47
+ if (this.isOkay)
48
+ return this;
49
+ if (that.isOkay)
50
+ return that;
51
+ return new Fail(new Pair(this.value, that.value));
52
+ }
53
+ orElse(that) {
54
+ return this.isOkay ? this : that;
55
+ }
56
+ orErst(that) {
57
+ return this.isOkay || that.isFail ? this : that;
58
+ }
59
+ flatMap(okayArrow, failArrow) {
60
+ return this.isOkay ? okayArrow(this.value) : failArrow(this.value);
61
+ }
62
+ flatMapOkay(arrow) {
63
+ return this.isOkay ? arrow(this.value) : this;
64
+ }
65
+ flatMapFail(arrow) {
66
+ return this.isFail ? arrow(this.value) : this;
67
+ }
68
+ flattenOkay() {
69
+ return this.isOkay ? this.value : this;
70
+ }
71
+ flattenFail() {
72
+ return this.isFail ? this.value : this;
73
+ }
74
+ flatMapUntil(okayArrow, failArrow) {
75
+ let result = this.flatMap(okayArrow, failArrow).distribute();
76
+ while (result.isFail)
77
+ result = result.value.flatMap(okayArrow, failArrow).distribute();
78
+ return result.value;
79
+ }
80
+ flatMapOkayUntil(arrow) {
81
+ let result = this.flatMapOkay(arrow).exchangeFail();
82
+ while (result.isFail)
83
+ result = arrow(result.value).exchangeFail();
84
+ return result.value;
85
+ }
86
+ flatMapFailUntil(arrow) {
87
+ let result = this.flatMapFail(arrow).associateLeft();
88
+ while (result.isFail)
89
+ result = arrow(result.value).associateLeft();
90
+ return result.value;
91
+ }
92
+ commute() {
93
+ return this.isOkay ? new Fail(this.value) : new Okay(this.value);
94
+ }
95
+ isOkayAnd(predicate) {
96
+ return this.isOkay && predicate(this.value);
97
+ }
98
+ isFailAnd(predicate) {
99
+ return this.isFail && predicate(this.value);
100
+ }
101
+ isOkayOr(predicate) {
102
+ return this.isOkay || predicate(this.value);
103
+ }
104
+ isFailOr(predicate) {
105
+ return this.isFail || predicate(this.value);
106
+ }
107
+ transposeMap(transposeOkay, transposeFail) {
108
+ return this.isOkay
109
+ ? transposeOkay(this.value).map(Okay.of)
110
+ : transposeFail(this.value).map(Fail.of);
111
+ }
112
+ transposeMapOkay(transpose) {
113
+ return this.isFail ? new Some(this) : transpose(this.value).map(Okay.of);
114
+ }
115
+ transposeMapFail(transpose) {
116
+ return this.isOkay ? new Some(this) : transpose(this.value).map(Fail.of);
117
+ }
118
+ transpose() {
119
+ return this.isOkay ? this.value.map(Okay.of) : this.value.map(Fail.of);
120
+ }
121
+ transposeOkay() {
122
+ return this.isFail ? new Some(this) : this.value.map(Okay.of);
123
+ }
124
+ transposeFail() {
125
+ return this.isOkay ? new Some(this) : this.value.map(Fail.of);
126
+ }
127
+ unzipWith(unzipOkay, unzipFail) {
128
+ return this.isOkay
129
+ ? unzipOkay(this.value).map(Okay.of, Okay.of)
130
+ : unzipFail(this.value).map(Fail.of, Fail.of);
131
+ }
132
+ unzipWithOkay(unzip) {
133
+ return this.isFail
134
+ ? Pair.from(this)
135
+ : unzip(this.value).map(Okay.of, Okay.of);
136
+ }
137
+ unzipWithFail(unzip) {
138
+ return this.isOkay
139
+ ? Pair.from(this)
140
+ : unzip(this.value).map(Fail.of, Fail.of);
141
+ }
142
+ unzip() {
143
+ return this.isOkay
144
+ ? this.value.map(Okay.of, Okay.of)
145
+ : this.value.map(Fail.of, Fail.of);
146
+ }
147
+ unzipOkay() {
148
+ return this.isFail ? Pair.from(this) : this.value.map(Okay.of, Okay.of);
149
+ }
150
+ unzipFail() {
151
+ return this.isOkay ? Pair.from(this) : this.value.map(Fail.of, Fail.of);
152
+ }
153
+ collectMapFst(okayMorphism, failMorphism) {
154
+ return this.isOkay
155
+ ? okayMorphism(this.value).mapFst(Okay.of)
156
+ : failMorphism(this.value).mapFst(Fail.of);
157
+ }
158
+ collectFst() {
159
+ return this.isOkay
160
+ ? this.value.mapFst(Okay.of)
161
+ : this.value.mapFst(Fail.of);
162
+ }
163
+ collectMapSnd(okayMorphism, failMorphism) {
164
+ return this.isOkay
165
+ ? okayMorphism(this.value).mapSnd(Okay.of)
166
+ : failMorphism(this.value).mapSnd(Fail.of);
167
+ }
168
+ collectSnd() {
169
+ return this.isOkay
170
+ ? this.value.mapSnd(Okay.of)
171
+ : this.value.mapSnd(Fail.of);
172
+ }
173
+ collectMapOkay(okayMorphism, failMorphism) {
174
+ return this.isOkay
175
+ ? okayMorphism(this.value).mapOkay(Okay.of)
176
+ : failMorphism(this.value).mapOkay(Fail.of);
177
+ }
178
+ exchangeMapFail(exchange) {
179
+ return this.isFail ? new Okay(this) : exchange(this.value).mapOkay(Okay.of);
180
+ }
181
+ associateMapLeft(morphism) {
182
+ return this.isOkay ? new Okay(this) : morphism(this.value).mapOkay(Fail.of);
183
+ }
184
+ collectOkay() {
185
+ return this.isOkay
186
+ ? this.value.mapOkay(Okay.of)
187
+ : this.value.mapOkay(Fail.of);
188
+ }
189
+ exchangeFail() {
190
+ return this.isFail ? new Okay(this) : this.value.mapOkay(Okay.of);
191
+ }
192
+ associateLeft() {
193
+ return this.isOkay ? new Okay(this) : this.value.mapOkay(Fail.of);
194
+ }
195
+ collectMapFail(okayMorphism, failMorphism) {
196
+ return this.isOkay
197
+ ? okayMorphism(this.value).mapFail(Okay.of)
198
+ : failMorphism(this.value).mapFail(Fail.of);
199
+ }
200
+ exchangeMapOkay(exchange) {
201
+ return this.isOkay ? new Fail(this) : exchange(this.value).mapFail(Fail.of);
202
+ }
203
+ associateMapRight(morphism) {
204
+ return this.isFail ? new Fail(this) : morphism(this.value).mapFail(Okay.of);
205
+ }
206
+ collectFail() {
207
+ return this.isOkay
208
+ ? this.value.mapFail(Okay.of)
209
+ : this.value.mapFail(Fail.of);
210
+ }
211
+ exchangeOkay() {
212
+ return this.isOkay ? new Fail(this) : this.value.mapFail(Fail.of);
213
+ }
214
+ associateRight() {
215
+ return this.isFail ? new Fail(this) : this.value.mapFail(Okay.of);
216
+ }
217
+ distributeMap(okayMorphism, failMorphism) {
218
+ return this.isOkay
219
+ ? okayMorphism(this.value).map(Okay.of, Okay.of)
220
+ : failMorphism(this.value).map(Fail.of, Fail.of);
221
+ }
222
+ distribute() {
223
+ return this.isOkay
224
+ ? this.value.map(Okay.of, Okay.of)
225
+ : this.value.map(Fail.of, Fail.of);
226
+ }
227
+ gatherMapOkay(okayMorphism, failMorphism) {
228
+ return this.isOkay
229
+ ? okayMorphism(this.value).mapOkay(Okay.of)
230
+ : failMorphism(this.value).mapOkay(Fail.of);
231
+ }
232
+ swapMapFail(morphism) {
233
+ return this.isFail
234
+ ? Task.okay(this)
235
+ : morphism(this.value).mapOkay(Okay.of);
236
+ }
237
+ groupMapLeft(morphism) {
238
+ return this.isOkay
239
+ ? Task.okay(this)
240
+ : morphism(this.value).mapOkay(Fail.of);
241
+ }
242
+ gatherOkay() {
243
+ return this.isOkay
244
+ ? this.value.mapOkay(Okay.of)
245
+ : this.value.mapOkay(Fail.of);
246
+ }
247
+ swapFail() {
248
+ return this.isFail ? Task.okay(this) : this.value.mapOkay(Okay.of);
249
+ }
250
+ groupLeft() {
251
+ return this.isOkay ? Task.okay(this) : this.value.mapOkay(Fail.of);
252
+ }
253
+ gatherMapFail(okayMorphism, failMorphism) {
254
+ return this.isOkay
255
+ ? okayMorphism(this.value).mapFail(Okay.of)
256
+ : failMorphism(this.value).mapFail(Fail.of);
257
+ }
258
+ swapMapOkay(morphism) {
259
+ return this.isOkay
260
+ ? Task.fail(this)
261
+ : morphism(this.value).mapFail(Fail.of);
262
+ }
263
+ groupMapRight(morphism) {
264
+ return this.isFail
265
+ ? Task.fail(this)
266
+ : morphism(this.value).mapFail(Okay.of);
267
+ }
268
+ gatherFail() {
269
+ return this.isOkay
270
+ ? this.value.mapFail(Okay.of)
271
+ : this.value.mapFail(Fail.of);
272
+ }
273
+ swapOkay() {
274
+ return this.isOkay ? Task.fail(this) : this.value.mapFail(Fail.of);
275
+ }
276
+ groupRight() {
277
+ return this.isFail ? Task.fail(this) : this.value.mapFail(Okay.of);
278
+ }
279
+ interchangeMap(okayMorphism, failMorphism) {
280
+ return this.isOkay
281
+ ? okayMorphism(this.value).map(Okay.of, Okay.of)
282
+ : failMorphism(this.value).map(Fail.of, Fail.of);
283
+ }
284
+ interchange() {
285
+ return this.isOkay
286
+ ? this.value.map(Okay.of, Okay.of)
287
+ : this.value.map(Fail.of, Fail.of);
288
+ }
289
+ extractOkay(defaultValue) {
290
+ return this.isOkay ? this.value : defaultValue;
291
+ }
292
+ extractFail(defaultValue) {
293
+ return this.isFail ? this.value : defaultValue;
294
+ }
295
+ extractMapOkay(getOkayValue) {
296
+ return this.isOkay ? this.value : getOkayValue(this.value);
297
+ }
298
+ extractMapFail(getFailValue) {
299
+ return this.isFail ? this.value : getFailValue(this.value);
300
+ }
301
+ toOptionOkay() {
302
+ return this.isOkay ? new Some(this.value) : None.instance;
303
+ }
304
+ toOptionFail() {
305
+ return this.isFail ? new Some(this.value) : None.instance;
306
+ }
307
+ toTask() {
308
+ return new Task((signal, callback) => {
309
+ if (!signal.aborted)
310
+ callback(this);
311
+ });
312
+ }
313
+ isSame(that) {
314
+ return this.isOkay
315
+ ? that.isOkay && this.value.isSame(that.value)
316
+ : that.isFail && this.value.isSame(that.value);
317
+ }
318
+ isNotSame(that) {
319
+ return this.isOkay
320
+ ? that.isFail || this.value.isNotSame(that.value)
321
+ : that.isOkay || this.value.isNotSame(that.value);
322
+ }
323
+ isLess(that) {
324
+ return this.isOkay
325
+ ? that.isOkay && this.value.isLess(that.value)
326
+ : that.isOkay || this.value.isLess(that.value);
327
+ }
328
+ isNotLess(that) {
329
+ return this.isOkay
330
+ ? that.isFail || this.value.isNotLess(that.value)
331
+ : that.isFail && this.value.isNotLess(that.value);
332
+ }
333
+ isMore(that) {
334
+ return this.isOkay
335
+ ? that.isFail || this.value.isMore(that.value)
336
+ : that.isFail && this.value.isMore(that.value);
337
+ }
338
+ isNotMore(that) {
339
+ return this.isOkay
340
+ ? that.isOkay && this.value.isNotMore(that.value)
341
+ : that.isOkay || this.value.isNotMore(that.value);
342
+ }
343
+ compare(that) {
344
+ return this.isFail
345
+ ? that.isOkay
346
+ ? new Some("<")
347
+ : this.value.compare(that.value)
348
+ : that.isFail
349
+ ? new Some(">")
350
+ : this.value.compare(that.value);
351
+ }
352
+ max(that) {
353
+ return this.isOkay
354
+ ? that.isFail
355
+ ? this
356
+ : new Okay(this.value.max(that.value))
357
+ : that.isOkay
358
+ ? that
359
+ : new Fail(this.value.max(that.value));
360
+ }
361
+ min(that) {
362
+ return this.isOkay
363
+ ? that.isFail
364
+ ? that
365
+ : new Okay(this.value.min(that.value))
366
+ : that.isOkay
367
+ ? this
368
+ : new Fail(this.value.min(that.value));
369
+ }
370
+ clamp(lower, upper) {
371
+ return this.max(lower).min(upper);
372
+ }
373
+ *okayValues() {
374
+ if (this.isOkay)
375
+ yield this.value;
376
+ }
377
+ *failValues() {
378
+ if (this.isFail)
379
+ yield this.value;
380
+ }
381
+ *values() {
382
+ yield this.value;
383
+ }
384
+ *effectMap(morphism) {
385
+ const value = yield this;
386
+ return morphism(value);
387
+ }
388
+ *effect() {
389
+ const value = yield this;
390
+ return value;
391
+ }
392
+ }
393
+ export class Okay extends ResultTrait {
394
+ value;
395
+ isOkay = true;
396
+ isFail = false;
397
+ constructor(value) {
398
+ super();
399
+ this.value = value;
400
+ }
401
+ static of(value) {
402
+ return new Okay(value);
403
+ }
404
+ // TODO: <A, B>(getGenerator: () => Generator<Result<A, unknown>, B, A>) => Result<B, unknown>
405
+ static fromGenerator(getGenerator) {
406
+ const generator = getGenerator();
407
+ let iteratorResult;
408
+ try {
409
+ iteratorResult = generator.next();
410
+ }
411
+ catch (error) {
412
+ return new Fail(error);
413
+ }
414
+ while (!iteratorResult.done) {
415
+ const result = iteratorResult.value;
416
+ try {
417
+ iteratorResult = result.isOkay
418
+ ? generator.next(result.value)
419
+ : generator.throw(result.value);
420
+ }
421
+ catch (error) {
422
+ return new Fail(error);
423
+ }
424
+ }
425
+ return new Okay(iteratorResult.value);
426
+ }
427
+ }
428
+ export class Fail extends ResultTrait {
429
+ value;
430
+ isOkay = false;
431
+ isFail = true;
432
+ constructor(value) {
433
+ super();
434
+ this.value = value;
435
+ }
436
+ static of(value) {
437
+ return new Fail(value);
438
+ }
439
+ }
@@ -0,0 +1,3 @@
1
+ export interface Semigroup<in out A> {
2
+ append: (this: A, that: A) => A;
3
+ }
@@ -0,0 +1 @@
1
+ export {};
package/lib/task.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import { Pair } from "./pair.js";
3
+ import type { Result } from "./result.js";
4
+ export declare class Task<out A, out E> {
5
+ readonly execute: (signal: AbortSignal, callback: (result: Result<A, E>) => void) => void;
6
+ constructor(execute: (signal: AbortSignal, callback: (result: Result<A, E>) => void) => void);
7
+ static okay<A>(value: A): Task<A, never>;
8
+ static fail<E>(value: E): Task<never, E>;
9
+ static of<A, E>(task: (callback: (result: Result<A, E>) => void) => (reason: unknown) => void): Task<A, E>;
10
+ static fromGenerator<A>(getGenerator: () => Generator<Task<unknown, unknown>, A, unknown>): Task<A, unknown>;
11
+ run<A, E>(this: Task<A, E>, callback: (result: Result<A, E>) => void): AbortController;
12
+ map<A, B, E, F>(this: Task<A, E>, okayMorphism: (value: A) => B, failMorphism: (value: E) => F): Task<B, F>;
13
+ mapOkay<A, B, E>(this: Task<A, E>, morphism: (value: A) => B): Task<B, E>;
14
+ mapFail<A, E, F>(this: Task<A, E>, morphism: (value: E) => F): Task<A, F>;
15
+ replace<A, B, E, F>(this: Task<A, E>, okayValue: B, failValue: F): Task<B, F>;
16
+ replaceOkay<A, B, E>(this: Task<A, E>, value: B): Task<B, E>;
17
+ replaceFail<A, E, F>(this: Task<A, E>, value: F): Task<A, F>;
18
+ and<A, B, E>(this: Task<A, E>, that: Task<B, E>): Task<Pair<A, B>, E>;
19
+ andThen<A, B, E>(this: Task<A, E>, that: Task<B, E>): Task<B, E>;
20
+ andWhen<A, B, E>(this: Task<A, E>, that: Task<B, E>): Task<A, E>;
21
+ or<A, E, F>(this: Task<A, E>, that: Task<A, F>): Task<A, Pair<E, F>>;
22
+ orElse<A, E, F>(this: Task<A, E>, that: Task<A, F>): Task<A, F>;
23
+ orErst<A, E, F>(this: Task<A, E>, that: Task<A, F>): Task<A, E>;
24
+ flatMap<A, B, E, F>(this: Task<A, E>, okayArrow: (value: A) => Task<B, F>, failArrow: (value: E) => Task<B, F>): Task<B, F>;
25
+ flatMapOkay<A, B, E>(this: Task<A, E>, arrow: (value: A) => Task<B, E>): Task<B, E>;
26
+ flatMapFail<A, E, F>(this: Task<A, E>, arrow: (value: E) => Task<A, F>): Task<A, F>;
27
+ flatten<A, E>(this: Task<Task<A, E>, Task<A, E>>): Task<A, E>;
28
+ flattenOkay<A, E>(this: Task<Task<A, E>, E>): Task<A, E>;
29
+ flattenFail<A, E>(this: Task<A, Task<A, E>>): Task<A, E>;
30
+ flatMapUntil<A, B, E, F>(this: Task<A, E>, okayArrow: (value: A) => Task<Result<B, A>, Result<F, E>>, failArrow: (value: E) => Task<Result<B, A>, Result<F, E>>): Task<B, F>;
31
+ flatMapOkayUntil<A, B, E>(this: Task<A, E>, arrow: (value: A) => Task<Result<B, A>, E>): Task<B, E>;
32
+ flatMapFailUntil<A, E, F>(this: Task<A, E>, arrow: (value: E) => Task<A, Result<F, E>>): Task<A, F>;
33
+ commute<A, B>(this: Task<A, B>): Task<B, A>;
34
+ effectMap<A, B, E>(this: Task<A, E>, morphism: (value: A) => B): Generator<Task<A, E>, B, A>;
35
+ effect<A, E>(this: Task<A, E>): Generator<Task<A, E>, A, A>;
36
+ }