forked from cullophid/barely-functional
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maybe.js
35 lines (33 loc) · 824 Bytes
/
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
function Nothing() {
return {
chain: f => Nothing(),
map: f => Nothing(),
ap: f => Nothing(),
concat: m => m,
empty: () => Nothing(),
isNothing: true,
isJust: false,
of: x => Just(x),
toString: () => 'Nothing()',
equals: m => m.isNothing === true
}
}
function Just(x) {
return {
chain: f => f(x),
map: f => Just(f(x)),
ap: m => m.map(x),
concat: m => m.isNothing === true ? Just(x) : Just(x.concat(m.value)),
empty: () => Nothing(),
isNothing: false,
isJust: true,
of: x => Just(x),
value: x,
toString: () => 'Just(' + x + ')',
equals: m => m.isJust === true && m.value === x
}
}
module.exports = {
Nothing: Nothing,
Just: Just
}