Skip to content

Instantly share code, notes, and snippets.

@Elyx0
Created June 11, 2017 23:01
Show Gist options
  • Save Elyx0/087874194dc04b800acd856ec5ead111 to your computer and use it in GitHub Desktop.
Save Elyx0/087874194dc04b800acd856ec5ead111 to your computer and use it in GitHub Desktop.
uncurry+mixins
// Loops over the list of functions and call
// the next with the previous result.
//
// If next is not a function,
// it's an additional argument to the previous result
const chain = (...fns) => start => fns
.reduce((res,next) => typeof next == 'function' ?
next(res) :
res(next)
,start);
// https://stackoverflow.com/questions/23482982/how-does-function-bind-bindfunction-call-uncurry
const uncurry = Function.bind.bind(Function.call);
// Split without arg transforms a string to an array
const toArr = uncurry(String.prototype.split);
const toUpperCase = uncurry(String.prototype.toUpperCase);
const join = who => what => who.join(what);
const push = who => what => who.concat(what);
const add = x => x+3;
const mult = x => x*2;
const res = chain(
mult,
add,
toArr,
push,
'candles',
join,
' ',
toUpperCase
)(12);
console.log(res); // 27 CANDLES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment