-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexample.js
57 lines (48 loc) · 1.92 KB
/
example.js
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
const Task = require('data.task')
const {IOType, IO} = require('./io')
const {Either, Left, Right} = require('./either')
const {Maybe, Just, Nothing} = require('./maybe')
const {ContType, Cont} = require('./cont')
const {State} = require('./state')
const {dispatch} = require('./interpret')
const Monad = require('./monad')
// some nt's, but where to stash these?
const maybeToTask = m => m.fold(Task.of, Task.rejected)
const contToTask = c => c.t
const eitherToTask = m => m.fold(Task.rejected, Task.of)
const ioToTask = i => new Task((rej, res) => res(i.f()))
// either Example
const gtZero = x =>
(x > 0) ? Right(x) : Left("it was less than zero")
// cont example (cont just wraps task. prob a misnomer)
const asyncGet = n =>
Cont((rej, res) => setTimeout(() => res(n), 100))
// do syntax works for any 1 monad. Since it's all in Free, we can use multiple
const app = Monad.do(function *() {
const ioNumber = yield IO(() => 1)
const maybeNumber = yield Just(2)
const contNumber = yield asyncGet(3)
const eitherNumber = yield gtZero(4)
return Monad.of(ioNumber + maybeNumber + eitherNumber + contNumber)
})
// this tells our Free.foldMap how to dispatch. We need all of them to turn into a target monad (in this case Task)
const runApp = dispatch([ [IOType, ioToTask],
[ContType, contToTask],
[Either, eitherToTask],
[Maybe, maybeToTask]
])
app.foldMap(runApp, Task.of).fork(console.error, console.log)
app.foldMap(runApp, Task.of).fork(console.error, console.log)
app.foldMap(runApp, Task.of).fork(console.error, console.log)
// 10
// 10
// 10
// do syntax is much nicer
gtZero(10)
.chain(ten =>
asyncGet(4).map(four =>
ten + four))
.foldMap(runApp, Task.of).fork(console.error, console.log)
// 14
gtZero(0).chain(() => asyncGet(4)).foldMap(runApp, Task.of).fork(console.error, console.log)
// error: it was less than zero