-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (117 loc) · 3.16 KB
/
index.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
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
import type from 'sanctuary-type-identifiers'
import * as F from 'fluture'
import Monet from 'monet'
const { Future } = F
const { Reader } = Monet
const I = v => v
const future = ({ _fT }) =>
F.isFuture(_fT) ? _fT : _fT[2].run(_fT[1].run(_fT[0](undefined)))
const chain = pred => ({ _fT }) => pred(_fT)
const coMap = pred => ft =>
F.isFuture(ft._fT)
? ft
: Futurity([
ft._fT[0],
ft._fT[1].flatMap(computed => Reader(e => pred(computed, e))),
ft._fT[2]
])
const envMap = pred => ft =>
F.isFuture(ft._fT)
? ft
: Futurity([e => pred(ft._fT[0](e)), ft._fT[1], ft._fT[2]])
const toString = ({ _fT }) => {
if (!F.isFuture(_fT)) {
return 'Futurity: [\n ' + _fT.toStgring() + ']'
}
const e = _fT[0]()
const a = _fT[1].run(e)
const b = _fT[2].run(a)
return (
'Futurity: [\n ' +
JSON.stringify(e) +
',\n ' +
a.toString() +
',\n ' +
b['@@show']() +
']'
)
}
export var $$type = 'fluture/Futurity@5'
function Futurity (_fT) {
return Object.create(Futurity.prototype, {
_fT: {
value: _fT,
writable: false,
enumerable: true,
configurable: true
}
})
}
const isFuturity = x => x instanceof Futurity || type(x) === $$type
const prototype = {
name: 'Futurity',
'@@type': $$type,
arity: 1,
_interpret: function (rec, rej, res) {
return future(this)._interpret(rec, rej, res)
},
_transform: function (transformation) {
return chain(_fT =>
Futurity(
F.isFuture(_fT)
? _fT._transform(transformation)
: [
_fT[0],
_fT[1],
_fT[2].map(f => f._transform(transformation))
]
)
)(this)
},
extractLeft: function Futurity$extractLeft () {
return F.isFuture(this._fT) ? this._fT.extractLeft() : []
},
extractRight: function Futurity$extractRight () {
return F.isFuture(this._fT) ? this._fT.extractRight() : []
}
}
Futurity.prototype = Object.create(Future.prototype, {
...Object.getOwnPropertyDescriptors(prototype),
constructor: {
value: Futurity,
enumerable: false,
writable: true
}
})
Futurity['@@type'] = $$type
Futurity.lift = (computation, computationToFluture) =>
Futurity([I, Reader(computation), Reader(computationToFluture)])
Futurity.ask = Reader
const resolve = v => Futurity(F.resolve(v))
const reject = v => Futurity(F.reject(v))
const rejectAfter = t => v => Futurity.lift(() => v, F.rejectAfter(t))
Futurity['fantasy-land/of'] = resolve
function Next (x) {
return { done: false, value: x }
}
function Done (x) {
return { done: true, value: x }
}
function chainRec (step, init) {
return Futurity(
F.Future['fantasy-land/chainRec']((n, d, v) => step(n, d, v)._fT, init)
)
}
Futurity['fantasy-land/chainRec'] = chainRec
export {
F,
Futurity,
future,
chain,
coMap,
envMap,
isFuturity,
resolve,
reject,
rejectAfter
}