forked from cbowdon/TsMonad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
maybe.ts
283 lines (260 loc) · 8.79 KB
/
maybe.ts
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/// <reference path="monad.ts" />
module TsMonad {
'use strict';
/**
* @name MaybeType
* @description Enumerate the different types contained by an Maybe object.
* @see Maybe#
*/
export enum MaybeType { Nothing, Just }
/**
* @name MaybePatterns
* @description Define a contract to unwrap Maybe object using callbacks
* for Just and Nothing.
* @see Maybe#
*/
export interface MaybePatterns<T,U> {
/**
* @name just
* @description Function to handle the Just.
* @type {(t: T) => U}
*/
just: (t: T) => U;
/**
* @name nothing
* @description Function to handle the Nothing.
* @type {() => U}
*/
nothing: () => U;
}
/**
* @name maybe
* @description Build a Maybe object.
* @function
* @param {T} t The object to wrap.
* @returns {Maybe<T>} A Maybe object containing the input. If t is null
* or undefined, the Maybe object is filled with Nothing.
* @see Maybe#
*/
export function maybe<T>(t: T) {
return Maybe.maybe(t);
}
/**
* @name Maybe
* @class Encapsulates an optional value. A value of type Maybe a either
* contains a value of type a (represented as Just a), or it is empty
* (represented as Nothing).
*/
export class Maybe<T> implements Monad<T>, Functor<T>, Eq<Maybe<T>> {
/**
* @description Build a Maybe object. For internal use only.
* @constructor
* @methodOf Maybe#
* @param {MaybeType} type Indicates if the Maybe content is a Just or a Nothing.
* @param {T} value The value to wrap (optional).
*/
constructor(private type: MaybeType,
private value?: T) {}
/**
* @name sequence
* @description Helper function to convert a map of Maybe objects into a Maybe of a map of objects.
* @methodOf Maybe#
* @static
* @param {{[id: string]: Maybe<T>}} t The value to unwrap Maybe values from.
* @returns {Maybe<{[id: string]: T}>} A Maybe object containing the value passed in input with fields unwrapped from Maybes.
*/
static sequence<T>(t: {[k: string]: Maybe<T>}): Maybe<{[k: string]: T}> {
if (Object.keys(t).filter(k => t[k].type === MaybeType.Nothing).length) {
return Maybe.nothing<{[k: string]: T}>();
}
var result: {[k: string]: any} = {};
for (var k in t) {
if (t.hasOwnProperty(k)) {
result[k] = t[k].value;
}
}
return Maybe.just(result);
}
/**
* @name all
* @description Alias for Maybe.sequence
* @methodOf Maybe#
* @static
* @see Maybe#sequence
*/
static all = (t: {[k: string]: Maybe<any>}) => Maybe.sequence<any>(t);
/**
* @name maybe
* @description Helper function to build a Maybe object.
* @methodOf Maybe#
* @static
* @param {T} t The value to wrap.
* @returns {Maybe<T>} A Maybe object containing the value passed in input. If t is null
* or undefined, the Maybe object is filled with Nothing.
*/
static maybe<T>(t: T) {
return t === null || t === undefined ?
new Maybe<T>(MaybeType.Nothing) :
new Maybe<T>(MaybeType.Just, t);
}
/**
* @name just
* @description Helper function to build a Maybe object filled with a
* Just type.
* @methodOf Maybe#
* @static
* @param {T} t The value to wrap.
* @returns {Maybe<T>} A Maybe object containing the value passed in input.
* @throws {TypeError} If t is null or undefined.
*/
static just<T>(t: T) {
if (t === null || t === undefined) {
throw new TypeError('Cannot Maybe.just(null)');
}
return new Maybe<T>(MaybeType.Just, t);
}
/**
* @name nothing
* @description Helper function to build a Maybe object filled with a
* Nothing type.
* @methodOf Maybe#
* @static
* @returns {Maybe<T>} A Maybe with a Nothing type.
*/
static nothing<T>() {
return new Maybe<T>(MaybeType.Nothing);
}
/**
* @name unit
* @description Wrap an object inside a Maybe.
* @public
* @methodOf Maybe#
* @param {U} u The object to wrap.
* @returns {Monad<U>} A Monad with the value wrapped inside.
* @see Monad#unit
*/
unit<U>(u: U) {
return Maybe.maybe<U>(u); // Slight deviation from Haskell, since sadly null does exist in JS
}
/**
* @name bind
* @description Apply the function passed as parameter on the object.
* @methodOf Maybe#
* @public
* @param {(t: T) => Maybe<U>} f Function applied on the Maybe content.
* @returns {Maybe<U>} The result of the function f wrapped inside
* a Maybe object.
* @see Monad#bind
*/
bind<U>(f: (t: T) => Maybe<U>) {
return this.type === MaybeType.Just ?
f(this.value) :
Maybe.nothing<U>();
}
/**
* @name of
* @description Alias for unit.
* @methodOf Maybe#
* @public
* @see Maybe#unit
* @see Monad#of
*/
of = this.unit;
/**
* @name chain
* @description Alias for bind.
* @methodOf Maybe#
* @public
* @see Maybe#unit
* @see Monad#of
*/
chain = this.bind;
/**
* @name fmap
* @description Apply the function passed as parameter on the object.
* @methodOf Maybe#
* @public
* @param {(t: T) => U} f Function applied on the Maybe content.
* @returns {Maybe<U>} The result of the function f wrapped inside
* an Maybe object.
* @see Functor#fmap
*/
fmap<U>(f: (t: T) => U) {
return this.bind(v => this.unit<U>(f(v)));
}
/**
* @name lift
* @description Alias for fmap.
* @methodOf Maybe#
* @public
* @see Maybe#fmap
* @see Monad#of
*/
lift = this.fmap;
/**
* @name map
* @description Alias for fmap.
* @methodOf Maybe#
* @public
* @see Maybe#fmap
* @see Monad#of
*/
map = this.fmap;
/**
* @name caseOf
* @description Execute a function depending on the Maybe content. It
* allows to unwrap the object for Just or Nothing types.
* @methodOf Maybe#
* @public
* @param {MaybePatterns<T, U>} pattern Object containing the
* functions to applied on each Maybe types.
* @return {U} The returned value of the functions specified in the
* MaybePatterns interface.
* @see MaybePatterns#
*/
caseOf<U>(patterns: MaybePatterns<T, U>) {
return this.type === MaybeType.Just ?
patterns.just(this.value) :
patterns.nothing();
}
/**
* @name defaulting
* @description Convert a possible Nothing into a guaranteed Maybe.Just.
* @methodOf Maybe#
* @public
* @param {T} pattern Default value to have if Nothing
* @return {Maybe<T>}
*/
defaulting(defaultValue: T) {
return Maybe.just(this.valueOr(defaultValue))
}
/**
* @name equals
* @description Compare the type and the content of two Maybe
* objects.
* @methodOf Maybe#
* @public
* @param {Maybe<T>} other The Maybe to compare with.
* @return {boolean} True if the type and content value are equals,
* false otherwise.
* @see Eq#equals
*/
equals(other: Maybe<T>) {
return other.type === this.type &&
(this.type === MaybeType.Nothing || eq(other.value, this.value));
}
/**
* @name valueOr
* @description Unwrap a Maybe with a default value
* @methodOf Maybe#
* @public
* @param {T} defaultValue Default value to have if Nothing
* @return {T}
* Separate U type to allow Maybe.nothing().valueOr() to match
* without explicitly typing Maybe.nothing.
*/
valueOr<U extends T>(defaultValue: U): T|U {
return this.type === MaybeType.Just ? this.value : defaultValue;
}
}
}