Created
May 12, 2014 14:06
-
-
Save ryanbriones/9d15f6c5970a75ad1a5e to your computer and use it in GitHub Desktop.
recursive 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
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