-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathMaybe.js
156 lines (113 loc) · 3.02 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
var toString = require('ramda/src/toString');
var curry = require('ramda/src/curry');
var util = require('./internal/util.js');
function Maybe(x) {
return x == null ? _nothing : Maybe.Just(x);
}
Maybe.prototype['@@type'] = 'ramda-fantasy/Maybe';
function Just(x) {
this.value = x;
}
util.extend(Just, Maybe);
Just.prototype.isJust = true;
Just.prototype.isNothing = false;
function Nothing() {}
util.extend(Nothing, Maybe);
Nothing.prototype.isNothing = true;
Nothing.prototype.isJust = false;
var _nothing = new Nothing();
Maybe.Nothing = function() {
return _nothing;
};
Maybe.Just = function(x) {
return new Just(x);
};
Maybe.of = Maybe.Just;
Maybe.prototype.of = Maybe.Just;
Maybe.isJust = function(x) {
return x.isJust;
};
Maybe.isNothing = function(x) {
return x.isNothing;
};
Maybe.maybe = curry(function(nothingVal, justFn, m) {
return m.reduce(function(_, x) {
return justFn(x);
}, nothingVal);
});
Maybe.toMaybe = Maybe;
// semigroup
Just.prototype.concat = function(that) {
return that.isNothing ? this : this.of(
this.value.concat(that.value)
);
};
Nothing.prototype.concat = util.identity;
// functor
Just.prototype.map = function(f) {
return this.of(f(this.value));
};
Nothing.prototype.map = util.returnThis;
// apply
// takes a Maybe that wraps a function (`app`) and applies its `map`
// method to this Maybe's value, which must be a function.
Just.prototype.ap = function(m) {
return m.map(this.value);
};
Nothing.prototype.ap = util.returnThis;
// applicative
// `of` inherited from `Maybe`
// chain
// f must be a function which returns a value
// f must return a value of the same Chain
// chain must return a value of the same Chain
Just.prototype.chain = util.baseMap;
Nothing.prototype.chain = util.returnThis;
//chainRec
Maybe.chainRec = Maybe.prototype.chainRec = function(f, i) {
var res, state = util.chainRecNext(i);
while (state.isNext) {
res = f(util.chainRecNext, util.chainRecDone, state.value);
if (Maybe.isNothing(res)) {
return res;
}
state = res.value;
}
return Maybe.Just(state.value);
};
//
Just.prototype.datatype = Just;
Nothing.prototype.datatype = Nothing;
// monad
// A value that implements the Monad specification must also implement the Applicative and Chain specifications.
// see above.
// equality method to enable testing
Just.prototype.equals = util.getEquals(Just);
Nothing.prototype.equals = function(that) {
return that === _nothing;
};
Maybe.prototype.isNothing = function() {
return this === _nothing;
};
Maybe.prototype.isJust = function() {
return this instanceof Just;
};
Just.prototype.getOrElse = function() {
return this.value;
};
Nothing.prototype.getOrElse = function(a) {
return a;
};
Just.prototype.reduce = function(f, x) {
return f(x, this.value);
};
Nothing.prototype.reduce = function(f, x) {
return x;
};
Just.prototype.toString = function() {
return 'Maybe.Just(' + toString(this.value) + ')';
};
Nothing.prototype.toString = function() {
return 'Maybe.Nothing()';
};
module.exports = Maybe;