Skip to content

Commit 3ce7d85

Browse files
committed
Added example from physics for ap function
1 parent ad1153a commit 3ce7d85

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

JavaScript/6-functor-ap-fp.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,24 @@ function maybe(x) {
1212
maybe(5)(x => x * 2)(x => ++x)(console.log);
1313
maybe(5)(x => x * 2).ap(maybe(x => ++x))(console.log);
1414
maybe(5).chain(x => maybe(x * 2))(x => ++x)(console.log);
15+
16+
const config = {
17+
coords: {
18+
x: 0,
19+
y: 5,
20+
},
21+
velocity: {
22+
x: 1,
23+
y: 1,
24+
},
25+
};
26+
27+
const addVelocity = velocity => coords => {
28+
coords.x += velocity.x;
29+
coords.y += velocity.y;
30+
return coords;
31+
};
32+
33+
const coords = maybe(config.coords);
34+
const velocity = maybe(config.velocity);
35+
coords.ap(velocity(addVelocity))(console.log);

0 commit comments

Comments
 (0)