/
eff.ts
183 lines (156 loc) · 5.45 KB
/
eff.ts
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import readline from "readline";
interface Effect<TResult> {
type: string
}
interface Log extends Effect<void> {
type: "log"
msg: string
}
interface Rand extends Effect<number> {
type: "rand"
min: number
max: number
}
interface Prompt extends Effect<string> {
type: "prompt"
msg: string
}
interface PromiseEffect extends Effect<unknown> {
type: "promise"
promise: Promise<unknown>
}
type Eff<TResult, TEffect extends Effect<TResult>> = Generator<TEffect, TResult, any>
function* effect<TResult, TEffect extends Effect<TResult>>(effect: TEffect): Eff<TResult, TEffect> {
return yield effect;
}
function log(msg: string): Eff<void, Log> {
return effect({ type: "log", msg: msg });
}
function rand(min: number, max: number): Eff<number, Rand> {
return effect({ type: "rand", min: min, max: max });
}
function prompt(msg: string): Eff<string, Prompt> {
return effect({ type: "prompt", msg: msg });
}
function* guessGame(): Eff<void, Log | Rand | Prompt> {
const secret = yield* rand(1, 100);
for(let i = 1; i <= 10; i++) {
const userInput = yield* prompt("Guess a number (1-100): ");
const guess = parseInt(userInput);
if (guess === secret) {
yield* log("Congrats!!!");
return;
}
else if (secret > guess)
yield* log("Secret is bigger!");
else
yield* log("Secret is smaller!");
}
yield* log(`GAME OVER - secret was ${secret}`);
}
function run(iter: Eff<void, Log | Rand | Prompt>) {
const internalLogs: unknown[] = [];
return new Promise((resolve, reject) => {
const result = iter.next();
function loop(result: IteratorResult<Log | Rand | Prompt, void>) {
internalLogs.push(result.value);
if (result.done)
resolve({ result: result.value, logs: internalLogs });
else {
const effect = result.value;
if (effect.type === "log") {
console.log(effect.msg);
loop(iter.next());
}
else if (effect.type === "rand") {
const randValue = Math.floor(Math.random() * effect.max) + effect.min;
loop(iter.next(randValue));
}
else if (effect.type === "prompt") {
const input = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
input.question(effect.msg, (value) => {
input.close();
loop(iter.next(value));
});
}
}
}
loop(result);
});
}
//const iter = guessGame();
//run(iter).then(x => console.log(x));
function* randHandler<TResult, TEffect extends Effect<TResult>>(iter: Eff<void, Rand | TEffect>): Eff<void, TEffect> {
let result = iter.next();
while (!result.done) {
const effect = result.value;
if (effect.type === "rand") {
const randEffect = effect as Rand;
const randValue = Math.floor(Math.random() * randEffect.max) + randEffect.min;
result = iter.next(randValue);
}
else {
const value = yield effect as TEffect;
result = iter.next(value);
}
}
}
function* logHandler<TResult, TEffect extends Effect<TResult>>(iter: Eff<void, Log | TEffect>): Eff<void, TEffect> {
let result = iter.next();
while (!result.done) {
const effect = result.value;
if (effect.type === "log") {
const logEffect = effect as Log;
console.log(logEffect.msg);
result = iter.next();
}
else {
const value = yield effect as TEffect;
result = iter.next(value);
}
}
}
function* promptHandler<TResult, TEffect extends Effect<TResult>>(iter: Eff<void, Prompt | TEffect>): Eff<void, PromiseEffect | TEffect> {
let result = iter.next();
while (!result.done) {
const effect = result.value;
if (effect.type === "prompt") {
const promptEffect = effect as Prompt;
function prompt(msg: string): Promise<string> {
return new Promise((resolve, reject) => {
const input = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
input.question(msg, (value) => {
input.close();
resolve(value);
});
});
}
const promise = prompt(promptEffect.msg);
const value = yield { type: "promise", promise: promise } as PromiseEffect;
result = iter.next(value);
}
else {
const value = yield effect as TEffect;
result = iter.next(value);
}
}
}
async function runHandler(iter: Eff<void, PromiseEffect>): Promise<void> {
let result = iter.next();
while (!result.done) {
const effect = result.value;
if (effect.type === "promise") {
const value = await effect.promise;
result = iter.next(value);
}
else
throw new Error(`invalid effect ${effect.type}`);
}
}
runHandler(logHandler(randHandler(promptHandler(guessGame()))));