Skip to content

Instantly share code, notes, and snippets.

@ryanbriones
Created May 12, 2014 14:06
Show Gist options
  • Save ryanbriones/9d15f6c5970a75ad1a5e to your computer and use it in GitHub Desktop.
Save ryanbriones/9d15f6c5970a75ad1a5e to your computer and use it in GitHub Desktop.
recursive compose
function square(n) {
return n * n;
}
function double(n) {
return n * 2;
}
function compose() {
var originalRecur = [];
for(var i = 0; i < arguments.length; i++) {
originalRecur.push(arguments[i]);
}
return function recurred() {
if(arguments[1] == undefined) {
return recurred.call(this, arguments[0], originalRecur);
}
var toRecur = arguments[1];
if(toRecur.length == 0) {
return arguments[0];
} else {
return recurred.call(this, toRecur[0](arguments[0]), toRecur.slice(1))
}
}
}
var squared_and_doubled = compose(function(n) { return n + 4 }, function(n) { return n + 5 });
console.log(squared_and_doubled(2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment