-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerators.ts
More file actions
36 lines (27 loc) · 1.04 KB
/
generators.ts
File metadata and controls
36 lines (27 loc) · 1.04 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
import { Effect, Array } from "effect";
const divide = (a: number, b: number) =>
Effect.suspend(() => {
if (b === 0) {
return Effect.fail(new Error("Division by zero"));
}
return Effect.succeed(a / b);
});
const fetchMultiplier = (average: number) => Effect.succeed(2); // imagine this fetches from API
const persist = (id: string, average: number, adjusted: number) => Effect.void; // imagine this writes to DB
const gen = Effect.gen(function* () {
const report = yield* Effect.sync(() => ({
id: "123",
timestamp: new Date().toISOString(),
values: [10, 20, 30, 40],
}));
if (Array.isEmptyArray(report.values)) {
return yield* Effect.fail(new Error("Empty array"));
}
const sum = report.values.reduce((a, b) => a + b, 0);
const average = yield* divide(sum, report.values.length);
yield* fetchMultiplier(average).pipe(
Effect.tap(multiplier => persist("123", average, average * multiplier))
);
return average;
}).pipe(Effect.orElseSucceed(() => 0));
Effect.runPromise(gen).then(console.log);