Last active
January 17, 2017 14:56
-
-
Save mintyPT/e7ebb4cd20573c89f48b073f7956b530 to your computer and use it in GitHub Desktop.
Compose functions into a new one
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
// 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