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 d5fbf11 commit ad1153aCopy full SHA for ad1153a
JavaScript/6-functor-ap-fp.js
@@ -1,22 +1,14 @@
1
'use strict';
2
3
-function maybe(x) {
4
- let map = fn => (x && fn) ? fn(x) : null;
5
-
6
- map.ap = map2 => (
7
- maybe(
8
- map(
9
- mbValue => (
10
- maybe(map2)(
11
- mbFunction => mbFunction(mbValue)
12
- )
13
14
15
16
- );
+const mapNull = (fn, x) => x ? fn(x) : null;
17
+function maybe(x) {
+ const map = fn => maybe(mapNull(fn, x));
+ map.ap = fnA => fnA(fn => mapNull(fn, x));
+ map.chain = fnM => fnM(x);
18
return map;
19
}
20
21
-maybe(5).ap(x => ++x)(console.log);
22
-maybe(5).ap(x => x * 2).ap(x => ++x)(console.log);
+maybe(5)(x => x * 2)(x => ++x)(console.log);
+maybe(5)(x => x * 2).ap(maybe(x => ++x))(console.log);
+maybe(5).chain(x => maybe(x * 2))(x => ++x)(console.log);
0 commit comments