From ad1153a03520e5e73fc651f85ce99800e174fb10 Mon Sep 17 00:00:00 2001 From: Dzyuba Vlad Date: Wed, 14 Dec 2016 00:49:44 +0200 Subject: [PATCH] Fixed app function, added chain --- JavaScript/6-functor-ap-fp.js | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/JavaScript/6-functor-ap-fp.js b/JavaScript/6-functor-ap-fp.js index f3b96c4..5b2895a 100644 --- a/JavaScript/6-functor-ap-fp.js +++ b/JavaScript/6-functor-ap-fp.js @@ -1,22 +1,14 @@ 'use strict'; -function maybe(x) { - let map = fn => (x && fn) ? fn(x) : null; - - map.ap = map2 => ( - maybe( - map( - mbValue => ( - maybe(map2)( - mbFunction => mbFunction(mbValue) - ) - ) - ) - ) - ); +const mapNull = (fn, x) => x ? fn(x) : null; +function maybe(x) { + const map = fn => maybe(mapNull(fn, x)); + map.ap = fnA => fnA(fn => mapNull(fn, x)); + map.chain = fnM => fnM(x); return map; } -maybe(5).ap(x => ++x)(console.log); -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);