Skip to content

Instantly share code, notes, and snippets.

@tincho
Last active January 9, 2017 20:30
Show Gist options
  • Save tincho/2d93dea725ddba88f31db6c71f504d65 to your computer and use it in GitHub Desktop.
Save tincho/2d93dea725ddba88f31db6c71f504d65 to your computer and use it in GitHub Desktop.
ES6 compose
// 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