-
Notifications
You must be signed in to change notification settings - Fork 2
/
Maybe.js
99 lines (73 loc) · 2.42 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
import { factorizeSumType } from "./SumType.js";
import { $$value } from "./Symbols.js";
// NOTE: Fix broken tests of IO
export const Maybe = factorizeSumType(
"Maybe",
{
Nothing: [],
Just: [ $$value ]
}
);
Maybe.fromNullable = value => !(typeof value !== "undefined") || !value && typeof value === "object"
? Maybe.nothing()
: Maybe.just(value);
Maybe.just = value => Maybe.Just(value);
Maybe.nothing = () => Maybe.Nothing;
Maybe.of = value => Maybe.Just(value);
Maybe.prototype.alt = Maybe.prototype["fantasy-land/alt"] = function (container) {
return this.fold({
Nothing: _ => container,
Just: _ => this
});
};
Maybe.prototype.ap = Maybe.prototype["fantasy-land/ap"] = function (container) {
if (Maybe.Nothing.is(this)) return this;
return Maybe.Just.is(container) ? Maybe.of(container[$$value](this[$$value])) : container;
};
Maybe.prototype.chain = Maybe.prototype["fantasy-land/chain"] = function (unaryFunction) {
return this.fold({
Nothing: _ => Maybe.Nothing,
Just: value => unaryFunction(value)
});
};
Maybe.prototype.extend = Maybe.prototype["fantasy-land/extend"] = function (unaryFunction) {
return this.fold({
Nothing: _ => Maybe.Nothing,
Just: _ => Maybe.of(unaryFunction(this))
});
};
Maybe.prototype.extract = Maybe.prototype["fantasy-land/extract"] = function () {
return this.fold({
Nothing: _ => Maybe.Nothing,
Just: value => value
});
};
Maybe.prototype.filter = Maybe.prototype["fantasy-land/filter"] = function (predicate) {
return this.fold({
Nothing: _ => this,
Just: value => predicate(value) ? this : Maybe.Nothing
});
};
Maybe.prototype.map = Maybe.prototype["fantasy-land/map"] = function (unaryFunction) {
return this.fold({
Nothing: _ => this,
Just: value => Maybe.of(unaryFunction(value))
});
};
Maybe.prototype.reduce = Maybe.prototype["fantasy-land/reduce"] = function (binaryFunction, accumulator) {
return this.fold({
Nothing: _ => accumulator,
Just: value => binaryFunction(accumulator, value)
});
};
Maybe.prototype.sequence = function (TypeRepresentation) {
return this.traverse(TypeRepresentation, x => x);
};
Maybe.prototype.traverse = Maybe.prototype["fantasy-land/traverse"] = function (TypeRepresentation, unaryFunction) {
return this.fold({
Nothing: _ => TypeRepresentation.of(Maybe.Nothing),
Just: value =>
unaryFunction(value).map(x => Maybe.Just(x))
});
};
export default Maybe;