Created
March 21, 2014 11:52
-
-
Save fricze/9684555 to your computer and use it in GitHub Desktop.
Neat JS 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
var reverseCall = function (argument, func) { | |
return func(argument); | |
}; | |
var compose = function (funcs) { | |
return function (argument) { | |
return funcs.reduce(reverseCall, argument); | |
}; | |
}; | |
var add1 = function (x) { return x + 1 }; | |
var add3 = function (x) { return x + 3 }; | |
var add5 = function (x) { return x + 5 }; | |
var add9 = compose([add1, add3, add5]); | |
add9(9); | |
var thread = function (funcs, argument) { | |
return compose(funcs)(argument); | |
}; | |
thread([add1, add1, add9, function half (x) { return x / 2 }], 9); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment