This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const program = (...list) => | |
| acc => | |
| R.flatten(list).reduce( (acc,fn) => acc.then(fn), Promise.resolve(acc)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const program = (...list) => acc => list.reduce( (acc,fn) => acc.then(fn), Promise.resolve(acc)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const bind = function(aPromise, f) { | |
| return aPromise.then(f); | |
| }, | |
| pipe = _.curry(function(functionList, aPromise) { | |
| return _.reduce(functionList, bind, aPromise); | |
| }), | |
| //even_cleaner_pipe = _.curry(_.partial(_.reduce,_, bind),2) | |
| program = function(/*...list of functions*/){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| getUserCommentsbyId = program( | |
| getUserbyIdAPI, // <-aysnc | |
| pick('commenterID'), // <-sync | |
| commentSearchAPI, // <-aysnc | |
| R.head, // <-sync | |
| R.tap(console.log.bind(console)) // <-sync, logs result to console | |
| ); | |
| // these each return a promise resolved with a user's latest comment based on a userID | |
| getUserCommentsbyId(345); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //alternatively, creating a custom reduce operation that explicitly wraps the accumulator, then gets curried | |
| const pipePromise2 = R.curry( | |
| (list,acc) => list.reduce( (acc,fn) => acc.then(fn), Promise.resolve(acc) ) | |
| ); | |
| //then, since we made our own reduce with the arguments how we wanted them, no need to R.flip | |
| const program = R.compose( pipePromise2, R.unapply(R.flatten) ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //using R.useWith to transform the arguments passed to pipePromise | |
| const pipePromisePlus = R.useWith(pipePromise, [acc => Promise.resolve(acc), R.identity]); | |
| const program = R.compose( R.flip(pipePromisePlus), R.unapply(R.flatten) ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| getUserCommentsbyId = program( | |
| getUserbyIdAPI, // <-aysnc | |
| pick('commenterID'), // <-sync | |
| commentSearchAPI, // <-aysnc | |
| R.head, // <-sync | |
| R.tap(console.log.bind(console)) // <-sync, logs result to console | |
| ); | |
| //these each return a promise resolved with a user's latest comment based on userID |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var reduce = fn => Arr => acc => Arr.reduce(fn, acc); | |
| var add = reduce((acc,x)=>acc+x);// -> fn | |
| add123From = add([1,2,3]);// -> fn | |
| add123From(0);// -> 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var reduce = function(fn){ | |
| return function(Arr, acc){ | |
| return Arr.reduce(fn,acc); | |
| } | |
| } | |
| var add = reduce(function(acc, x){ | |
| return acc+x; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var reduce = fn => (Arr, acc) => Arr.reduce(fn, acc); | |
| var add = reduce((acc,x)=>acc+x); | |
| add([1,2,3],0);// -> 6 |