-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposition.ts
More file actions
30 lines (26 loc) · 899 Bytes
/
composition.ts
File metadata and controls
30 lines (26 loc) · 899 Bytes
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
import { timestamp } from "@effect/cluster/Snowflake";
import { Array, Effect } from "effect";
import { LengthSchemaId } from "effect/Schema";
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 program = Effect.sync(() => ({
id: "report-123",
timestamp: new Date().toISOString(),
// values: [10, 20, 30, 40],
values: Array.empty<number>(),
})).pipe(
Effect.map(data => data.values),
Effect.flatMap(values => {
const sum = values.reduce((acc, value) => acc + value, 0);
return divide(sum, values.length);
}),
Effect.orElseSucceed(() => 0)
// Effect.flatMap(data => divide(data.sum, data.length)),
// Effect.filterOrFail(Array.isNonEmptyArray, () => new Error("Empty Array")),
);
Effect.runPromise(program).then(console.log);