-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path35-io-either.ts
More file actions
106 lines (100 loc) · 2.92 KB
/
35-io-either.ts
File metadata and controls
106 lines (100 loc) · 2.92 KB
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
import * as Apply from "fp-ts/Apply";
import * as E from "fp-ts/Either";
import { pipe } from "fp-ts/function";
import * as IOE from "fp-ts/IOEither";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
describe("ApplicativePar", () => {
// From: https://github.com/gcanti/fp-ts/blob/3.0.0/test/IOEither.ts
// Also: https://github.com/gcanti/fp-ts/commit/530570d63c59e548d8f70849b062fec62de93664#diff-7f5d7d5a53083e763343923c48693b7d18711458fd373aa9405c87a87a4f201d
// This is the commit where `sequenceT` was removed
it("example", () => {
const log: Array<string> = [];
const a = IOE.rightIO<number, string>(() => log.push("a"));
const b = IOE.leftIO<string, number>(() => {
log.push("b");
return "error";
});
const c = IOE.rightIO<number, string>(() => log.push("c"));
expect(
pipe(
IOE.ApT,
IOE.apT(a),
IOE.apT(b),
IOE.apT(c), //
)(),
).toEqual(E.left("error"));
expect(
log, //
).toEqual(["a", "b", "c"]); // <- All tasks executed
// Extra: this is the pipe used in the official repo
const tuple =
<A>(a: A) =>
<B>(b: B) =>
<C>(c: C): readonly [A, B, C] =>
[a, b, c];
const A = IOE.ApplicativePar;
expect(
pipe(
a,
A.map(tuple),
A.ap(b),
A.ap(c), //
)(),
).toEqual(
pipe(
IOE.ApT,
IOE.apT(a),
IOE.apT(b),
IOE.apT(c), //
)(),
);
});
});
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
describe("ApplicativeSeq", () => {
// From: https://github.com/gcanti/fp-ts/blob/3.0.0/test/IOEither.ts
// Also: https://github.com/gcanti/fp-ts/commit/530570d63c59e548d8f70849b062fec62de93664#diff-7f5d7d5a53083e763343923c48693b7d18711458fd373aa9405c87a87a4f201d
// This is the commit where `sequenceT` was removed
it("example", () => {
const log: Array<string> = [];
const a = IOE.rightIO<number, string>(() => log.push("a"));
const b = IOE.leftIO<string, number>(() => {
log.push("b");
return "error";
});
const c = IOE.rightIO<number, string>(() => log.push("c"));
expect(
pipe(
IOE.ApT,
Apply.apT(IOE.ApplySeq)(a),
Apply.apT(IOE.ApplySeq)(b),
Apply.apT(IOE.ApplySeq)(c),
)(),
).toEqual(E.left("error"));
expect(
log, //
).toEqual(["a", "b"]); // <- Tasks stopped after `b` returned `left`
// Extra: this is the pipe used in the official repo
const tuple =
<A>(a: A) =>
<B>(b: B) =>
<C>(c: C): readonly [A, B, C] =>
[a, b, c];
const A = IOE.ApplicativeSeq;
expect(
pipe(
a,
A.map(tuple),
A.ap(b),
A.ap(c), //
)(),
).toEqual(
pipe(
IOE.ApT,
Apply.apT(IOE.ApplySeq)(a),
Apply.apT(IOE.ApplySeq)(b),
Apply.apT(IOE.ApplySeq)(c), //
)(),
);
});
});