-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmonad.js
More file actions
57 lines (46 loc) · 1.22 KB
/
monad.js
File metadata and controls
57 lines (46 loc) · 1.22 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
'use strict';
class Monad {
#value;
constructor(value) {
this.#value = value;
}
static of(value) {
return new Monad(value);
}
map(fn) {
const value = structuredClone(this.#value);
return Monad.of(fn(value));
}
chain(fn) {
const value = structuredClone(this.#value);
return fn(value);
}
ap(container) {
const fn = this.#value;
return container.map(fn);
}
tap(fn) {
fn(this.#value);
return this;
}
}
const createPoint = ({ x, y }) => {
const errors = [];
if (!Number.isFinite(x)) errors.push(new TypeError(`Invalid x: ${x}`));
if (!Number.isFinite(y)) errors.push(new TypeError(`Invalid y: ${y}`));
if (errors.length > 0) {
const cause = new AggregateError(errors, 'Validation');
throw new RangeError('Bad coordinates', { cause });
}
return { x, y };
};
const move = (d) => (p) => ({ x: p.x + d.x, y: p.y + d.y });
const clone = ({ x, y }) => createPoint({ x, y });
const toString = ({ x, y }) => Monad.of(`(${x}, ${y})`);
// Usage
const p1 = Monad.of(createPoint({ x: 10, y: 20 }));
p1.chain(toString).tap(console.log);
const c0 = p1.map(clone);
const t1 = Monad.of(move({ x: -5, y: 10 }));
const c1 = t1.ap(c0);
c1.chain(toString).tap(console.log);