Skip to content

Instantly share code, notes, and snippets.

@fricze
Created March 21, 2014 11:52
Show Gist options
  • Save fricze/9684555 to your computer and use it in GitHub Desktop.
Save fricze/9684555 to your computer and use it in GitHub Desktop.
Neat JS compose
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