-
Notifications
You must be signed in to change notification settings - Fork 4
/
maybe.js
202 lines (183 loc) · 3.84 KB
/
maybe.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
function isNull(ref) {
return ref === undefined || ref === null
}
function isMaybe(m) {
return m instanceof MaybeDef
}
function isNone(m) {
return m === None
}
class MaybeDef {
constructor(ref) {
this.ref = ref
}
of(ref) {
if (isNull(ref) || isNone(ref)) {
return None
}
var m = new MaybeDef(ref)
return m
}
fromFalsy(ref) {
return ref ? this.of(ref) : None
}
fromPredicate(predicate, value) {
if (arguments.length > 1) {
return predicate(value) ? this.of(value) : None
}
return (x) => this.fromPredicate(predicate, x)
}
isNull() {
// return isNull(this.ref)
return false
}
isPresent() {
// return !this.isNull()
return true
}
unwrap() {
return this.ref
}
toString() {
// return `Maybe(${JSON.stringify(this.ref)})`
return `Some(${JSON.stringify(this.ref)})`
}
toList() {
return [this.ref]
}
empty() {
return None
}
zero() {
return None
}
or(ref) {
// return this.isNull() ? this.of(ref) : this
return this
}
orDo(fn) {
// if (this.isNull()) {
// // return this.then(fn);
// // NOTE: It's expectable: null cases
// return this.of(fn());
// }
return this
}
letDo(fn) {
// if (this.isPresent()) {
// return this.then(fn);
// }
// return this
return this.then(fn)
}
then(fn) {
return this.of(this.flatMap(fn))
}
flatMap(fn) {
return fn(this.ref)
}
join() {
let ref = this.ref
if (isMaybe(ref)) {
return ref.join()
}
return this
}
reduce(reducer, initVal) {
return reducer(initVal, this.ref)
}
filter(predicate) {
return predicate(this.ref) ? this.of(this.ref) : None
}
ap(fnM) {
return fnM.chain(f => this.map(f))
}
chainRec (f, i) {
let result
let x = i
do {
result = f((x) => {return {value: x, done: false}}, (x) => {return {value: x, done: true}}, x).unwrap()
x = result.value
} while (!result.done)
return this.of(result.value)
}
equals(m) {
return isMaybe(m) && m.unwrap() === this.ref
}
}
// Expectable cases of Null
var None = Object.assign(new MaybeDef(), {
isNull: function() {
return true
},
isPresent: function() {
return false
},
unwrap: function() {
return null
},
toString: function() {
return 'None'
},
toList: function() {
return []
},
or: function(ref) {
return this.of(ref)
},
orDo: function(fn) {
return this.of(fn())
},
letDo: function(fn) {
return this
},
join: function() {
return None
},
reduce: function(reducer, initVal) {
return initVal
},
filter: function() {
return None
},
ap: function(fnM) {
return None
},
equals: function(m) {
return isNone(m)
},
})
// Prevent avoiding aliases in case of leaking.
const aliases = {
'just': 'of',
'chain': 'flatMap',
'bind': 'then',
'map': 'then',
'alt': 'or',
'extend': 'letDo',
'extract': 'unwrap',
'fantasy-land/of': 'of',
'fantasy-land/empty': 'empty',
'fantasy-land/zero': 'zero',
'fantasy-land/extract': 'extract',
'fantasy-land/equals': 'equals',
'fantasy-land/map': 'map',
'fantasy-land/ap': 'ap',
'fantasy-land/alt': 'alt',
'fantasy-land/chain': 'chain',
'fantasy-land/join': 'join',
'fantasy-land/extend': 'extend',
'fantasy-land/reduce': 'reduce',
'fantasy-land/filter': 'filter',
}
Object.keys(aliases).forEach((key) => {
MaybeDef.prototype[key] = MaybeDef.prototype[aliases[key]]
});
// [MaybeDef].forEach((classInstance) => {
// classInstance.prototype.alt = classInstance.prototype.or
// classInstance.prototype.extend = classInstance.prototype.letDo
// classInstance.prototype.extract = classInstance.prototype.unwrap
// })
// NOTE: There's only one class for speed-up purposes (ES5 code-gen & .js file size)
var Maybe = new MaybeDef({})
module.exports = Maybe