forked from gcanti/fp-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.ts
53 lines (47 loc) · 1.17 KB
/
15.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
//
// Code for http://www.tomharding.me/2017/06/05/fantas-eel-and-specification-15/
//
import { Task, task } from '../src/Task'
import { createInterface } from 'readline'
const rl = createInterface({
input: process.stdin,
output: process.stdout
})
const prompt = new Task<string>(
() =>
new Promise(res => {
rl.question('> ', res)
})
)
const speak = (message: string) =>
new Task<void>(
() =>
new Promise(res => {
res(console.log(message))
})
)
// MyApp :: Task<string>
const MyApp =
// Get the name...
speak('What is your name?')
.chain(_ => prompt)
.chain(name =>
// Get the age...
speak('And what is your age?')
.chain(_ => prompt)
.chain(
age =>
// Do the logic...
parseInt(age, 10) > 30
? speak('Seriously, ' + name + '?!').chain(_ =>
speak(`You don't look a day over ` + (parseInt(age, 10) - 10) + '!')
)
: speak('Hmm, I can believe that!')
)
// Return the name!
.chain(_ => task.of(name))
)
MyApp.run().then(name => {
console.log('FLATTERED ' + name)
rl.close()
})