We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8d36d8f commit 33e8b73Copy full SHA for 33e8b73
JavaScript/5-proto-ap.js
@@ -5,18 +5,19 @@ function Maybe(x) {
5
}
6
7
Maybe.prototype.map = function(fn) {
8
- return (this.x && fn) ? fn(this.x) : null;
+ const res = (this.x && fn) ? fn(this.x) : null;
9
+ return res instanceof Maybe ? res : new Maybe(res);
10
};
11
-Maybe.prototype.ap = function(maybe) {
12
- return new Maybe(this.map(mbValue => (maybe.map(
13
- mbFunction => mbFunction(mbValue)
14
- ))));
+Maybe.prototype.ap = function(functor) {
+ return this.map(val => functor.map(f => f(val)));
15
16
17
// Usage
18
19
const a = new Maybe(5);
20
const f1 = new Maybe(x => x * 2);
21
const f2 = new Maybe(x => ++x);
+
22
a.ap(f1).ap(f2).map(console.log);
23
0 commit comments