Created
June 11, 2017 23:01
-
-
Save Elyx0/087874194dc04b800acd856ec5ead111 to your computer and use it in GitHub Desktop.
uncurry+mixins
This file contains 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
// 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