Last active
January 9, 2017 20:30
-
-
Save tincho/2d93dea725ddba88f31db6c71f504d65 to your computer and use it in GitHub Desktop.
ES6 compose
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
// both answers copied from challange posted at e-mail list of my former job | |
// not-so oneliner | |
var compose = function () { | |
var composableFunctions = Array.prototype.slice.call(arguments).reverse(); | |
return (x) => composableFunctions.reduce((result, composable) => composable(result), x); | |
/* | |
ES5: | |
return function(x) { | |
return composableFunctions.reduce(function(result, composable) { | |
return composable(result) | |
}, x); | |
}*/ | |
} | |
// one-liner | |
var compose = (...fs) => (x) => fs.reduce((p,f) => f(p), x); | |
/* | |
ES5: | |
var compose = function() { | |
var fs = Array.prototype.slice.call(arguments); | |
return function(x) { | |
return fs.reduce(function(p, f) { | |
return f(p); | |
}, x); | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment