Skip to content

Instantly share code, notes, and snippets.

View dtipson's full-sized avatar

Drew dtipson

View GitHub Profile
@dtipson
dtipson / program in one line plus flatten.js
Created November 7, 2015 21:35
Program in one line plus flatten
const program = (...list) =>
acc =>
R.flatten(list).reduce( (acc,fn) => acc.then(fn), Promise.resolve(acc));
@dtipson
dtipson / program in one line.js
Created November 7, 2015 21:30
ES2015 version of program
const program = (...list) => acc => list.reduce( (acc,fn) => acc.then(fn), Promise.resolve(acc));
@dtipson
dtipson / Composing functions with Promises.js
Created November 7, 2015 20:45
Composing functions with Promises
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*/){
@dtipson
dtipson / Silly program example 2.js
Last active March 26, 2018 09:39
Silly program example #2
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);
@dtipson
dtipson / enhanced-pipe-promise.js
Created November 6, 2015 21:35
Enhanced Pipe Promise #2
//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) );
@dtipson
dtipson / enhanced-pipe-promise.js
Last active November 6, 2015 21:35
Enhanced Pipe Promise
//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) );
@dtipson
dtipson / program-example.js
Last active November 7, 2015 21:43
Silly Program example
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
@dtipson
dtipson / Partially applied reduce with arrow functions.js
Created November 5, 2015 17:00
Partially applied reduce with arrow functions again
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
@dtipson
dtipson / Partially applied reduce.js
Last active November 7, 2015 21:07
Partially applied reduce.js
var reduce = function(fn){
return function(Arr, acc){
return Arr.reduce(fn,acc);
}
}
var add = reduce(function(acc, x){
return acc+x;
});
@dtipson
dtipson / Partially applied reduce with arrow functions.js
Created November 5, 2015 16:45
Partially applied reduce with arrow functions.js
var reduce = fn => (Arr, acc) => Arr.reduce(fn, acc);
var add = reduce((acc,x)=>acc+x);
add([1,2,3],0);// -> 6