Skip to content

Commit 4d3d944

Browse files
authored
Merge pull request #4 from DzyubSpirit/master
Fixed app function, added chain
2 parents d5fbf11 + ad1153a commit 4d3d944

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

JavaScript/6-functor-ap-fp.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
'use strict';
22

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-
);
3+
const mapNull = (fn, x) => x ? fn(x) : null;
174

5+
function maybe(x) {
6+
const map = fn => maybe(mapNull(fn, x));
7+
map.ap = fnA => fnA(fn => mapNull(fn, x));
8+
map.chain = fnM => fnM(x);
189
return map;
1910
}
2011

21-
maybe(5).ap(x => ++x)(console.log);
22-
maybe(5).ap(x => x * 2).ap(x => ++x)(console.log);
12+
maybe(5)(x => x * 2)(x => ++x)(console.log);
13+
maybe(5)(x => x * 2).ap(maybe(x => ++x))(console.log);
14+
maybe(5).chain(x => maybe(x * 2))(x => ++x)(console.log);

0 commit comments

Comments
 (0)