-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path33-io.ts
More file actions
29 lines (20 loc) · 709 Bytes
/
33-io.ts
File metadata and controls
29 lines (20 loc) · 709 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
import { pipe } from "fp-ts/function";
import * as IO from "fp-ts/IO";
import * as A from "fp-ts/ReadonlyArray";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
describe("Examples", () => {
it("Append to mutable db", () => {
const db: { ns: ReadonlyArray<number> } = { ns: [1, 2, 3] };
const getDb: IO.IO<ReadonlyArray<number>> = () => db.ns;
const setDb =
(ns: ReadonlyArray<number>): IO.IO<void> =>
() =>
(db.ns = ns);
const appendToDb = (n: number) =>
pipe(getDb, IO.map(A.append(n)), IO.chain(setDb));
const exec = appendToDb(4);
db.ns = [6, 7, 8];
exec();
expect(db.ns).toEqual([6, 7, 8, 4]);
});
});