We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0affa78 commit c36cf20Copy full SHA for c36cf20
JavaScript/maybe3.js
@@ -5,18 +5,16 @@ function Maybe(x) {
5
}
6
7
Maybe.prototype.map = function(fn) {
8
- if (this.x && fn) {
9
- return new Maybe(fn(this.x));
10
- } else {
11
- return new Maybe(null);
12
- }
+ return (this.x && fn) ? fn(this.x) : null;
13
};
14
15
Maybe.prototype.ap = function(maybe) {
16
- return maybe.map(this.x);
+ return new Maybe(this.map(mbValue => (maybe.map(
+ mbFunction => mbFunction(mbValue)
+ ))));
17
18
19
let a = new Maybe(5);
20
-let b = new Maybe(x => x * 2);
21
-let c = b.ap(a).map(console.log);
22
-console.log(c);
+let f1 = new Maybe(x => x * 2);
+let f2 = new Maybe(x => ++x);
+a.ap(f1).ap(f2).map(console.log);
0 commit comments