-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathio-alt.ts
More file actions
87 lines (66 loc) · 1.74 KB
/
io-alt.ts
File metadata and controls
87 lines (66 loc) · 1.74 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
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
'use strict';
export {};
class Effect<T> {
private readonly command: () => T;
private constructor(command: () => T) {
this.command = command;
}
static from<T>(command: () => T): Effect<T> {
return new Effect(command);
}
transform<U>(fn: (value: T) => U): Effect<U> {
return new Effect(() => fn(this.command()));
}
chain<U>(fn: (value: T) => Effect<U>): Effect<U> {
return new Effect(() => fn(this.command()).execute());
}
execute(): T {
return this.command();
}
}
class ValueBox<T> {
private readonly value: T;
private constructor(value: T) {
this.value = value;
}
static from<T>(value: T): ValueBox<T> {
return new ValueBox(value);
}
transform<U>(fn: (v: T) => U): ValueBox<U> {
return new ValueBox(fn(this.clone()));
}
chain<R>(fn: (v: T) => R): R {
return fn(this.clone());
}
apply<A, B>(this: ValueBox<(arg: A) => B>, other: ValueBox<A>): ValueBox<B> {
return other.transform(this.value);
}
unwrap(): T {
return this.clone();
}
private clone(): T {
return structuredClone(this.value);
}
}
interface Point {
x: number;
y: number;
}
const move =
(d: Point) =>
(p: Point): Point => ({ x: p.x + d.x, y: p.y + d.y });
const clone = ({ x, y }: Point): Point => ({ x, y });
const toString = ({ x, y }: Point) => Effect.from(() => `(${x}, ${y})`);
// Usage
const start = Effect.from(() => ValueBox.from<Point>({ x: 10, y: 20 }));
start
.chain((box) => box.chain(toString))
.transform(console.log)
.execute();
const c0 = start.chain((b) => Effect.from(() => b.transform(clone)));
const c1 = c0.chain((b) =>
Effect.from(() => ValueBox.from(move({ x: -5, y: 10 })).apply(b)),
);
c1.chain((b) => b.chain(toString))
.transform(console.log)
.execute();