Skip to content

Instantly share code, notes, and snippets.

@mintyPT
Last active January 17, 2017 14:56
Show Gist options
  • Save mintyPT/e7ebb4cd20573c89f48b073f7956b530 to your computer and use it in GitHub Desktop.
Save mintyPT/e7ebb4cd20573c89f48b073f7956b530 to your computer and use it in GitHub Desktop.
Compose functions into a new one
// http://scott.sauyet.com/Javascript/Talk/Compose/2013-05-22/#slide-17
var compose = function() {
var funcs = arguments;
return function() {
var args = arguments;
for (var i = funcs.length; i --> 0;) {
args = [funcs[i].apply(this, args)];
}
return args[0];
};
};
// example
const add1 = x => x + 1;
const double = x => 2 * x
const f = compose(double, add1);
console.log(f(100));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment