-
Notifications
You must be signed in to change notification settings - Fork 25
/
18-maybe-monad.js
47 lines (40 loc) · 1.2 KB
/
18-maybe-monad.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
// goal: multiply each __even__ number and print the result
var Maybe = require('data.maybe');
var numbers = [3, 2, 6, 7];
var constant = 2;
function mul(a, b) {
return a * b;
}
function print(n) {
console.log(n);
}
function isEven(n) {
return n % 2 === 0;
}
var byConstant = mul.bind(null, constant);
function forEach(cb, array) {
for (var k = 0; k < array.length; k += 1) {
cb(array[k]);
}
}
/*
imperative solution would just use the "IF/ELSE"
if (isEven(x)) {
print(byConstant(x))
}
instead we are going to put the number in a wrapper Maybe
if the number is even we are going to have wrapper Maybe.Just
and the Maybe.Just().map(f) will call "f" passing the value inside
On the other hand, any value placed into Maybe.Nothing
is lost - Maybe.Nothing().map(f) will NOT call "f"
*/
const maybeEven = n => isEven(n) ? Maybe.Just(n) : Maybe.Nothing();
const process = n => maybeEven(n).map(byConstant).map(print);
/*
3 ----- Nothing()
2 ----- Just(2)------- Just(4) ------ "4" (Just(4) continues)
6 ----- Just(6)------- Just(12) ----- "12" (Just(12) continues)
7 ----- Nothing()
*/
forEach(process, numbers);
// 4 12