-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10-apply-ap.ts
92 lines (81 loc) · 2.38 KB
/
10-apply-ap.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
import { pipe } from "fp-ts/function";
import * as S from "fp-ts/string";
import * as T from "fp-ts/Task";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
const logs: Array<string> = [];
const task = (name: string, millis: number): T.Task<string> =>
pipe(
T.of(name),
T.delay(millis),
T.chainFirst(() => T.fromIO(() => logs.push(name))),
);
beforeEach(() => {
logs.length = 0;
});
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
describe("f...*.ap (parallel)", () => {
it("Option 1", async () => {
expect(
await pipe(
T.of(S.Semigroup.concat), // Task<string -> string -> string>
T.ap(task("fast", 0)),
T.ap(task("slow", 20)), //
)(),
).toEqual("slowfast");
expect(logs).toEqual(["fast", "slow"]);
});
it("Option 2", async () => {
expect(
await pipe(
T.of(S.Semigroup.concat),
T.ApplyPar.ap(task("fast", 0)),
T.ApplyPar.ap(task("slow", 20)), //
)(),
).toEqual("slowfast");
expect(logs).toEqual(["fast", "slow"]);
});
it("Option 3", async () => {
expect(
await pipe(
T.of(S.Semigroup.concat),
T.ApplicativePar.ap(task("fast", 0)),
T.ApplicativePar.ap(task("slow", 20)), //
)(),
).toEqual("slowfast");
expect(logs).toEqual(["fast", "slow"]);
});
});
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
describe("f...*.ap (sequential)", () => {
// NOTE: *.apSeq is not exported, so Option 1 is not available in this case.
// it("Option 1", async () => {
// expect(
// await pipe(
// T.of(S.Semigroup.concat),
// T.apSeq(task("slow", 20)),
// T.apSeq(task("fast", 0)), //
// )(),
// ).toEqual("slowfast");
// expect(logs).toEqual(["slow", "fast"]);
// });
it("Option 2", async () => {
expect(
await pipe(
T.of(S.Semigroup.concat),
T.ApplySeq.ap(task("slow", 20)),
T.ApplySeq.ap(task("fast", 0)), //
)(),
).toEqual("fastslow");
expect(logs).toEqual(["slow", "fast"]);
});
it("Option 3", async () => {
expect(
await pipe(
T.of(S.Semigroup.concat),
T.ApplicativeSeq.ap(task("slow", 20)),
T.ApplicativeSeq.ap(task("fast", 0)), //
)(),
).toEqual("fastslow");
expect(logs).toEqual(["slow", "fast"]);
});
});