From 3ce7d85feeea6a5e90fcd8da7cd683aed3fc3638 Mon Sep 17 00:00:00 2001 From: Dzyuba Vlad Date: Wed, 14 Dec 2016 01:43:04 +0200 Subject: [PATCH] Added example from physics for ap function --- JavaScript/6-functor-ap-fp.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/JavaScript/6-functor-ap-fp.js b/JavaScript/6-functor-ap-fp.js index 5b2895a..45531e6 100644 --- a/JavaScript/6-functor-ap-fp.js +++ b/JavaScript/6-functor-ap-fp.js @@ -12,3 +12,24 @@ function maybe(x) { 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); + +const config = { + coords: { + x: 0, + y: 5, + }, + velocity: { + x: 1, + y: 1, + }, +}; + +const addVelocity = velocity => coords => { + coords.x += velocity.x; + coords.y += velocity.y; + return coords; +}; + +const coords = maybe(config.coords); +const velocity = maybe(config.velocity); +coords.ap(velocity(addVelocity))(console.log);