Created
October 20, 2013 03:10
-
-
Save THEtheChad/7064524 to your computer and use it in GitHub Desktop.
Curry & AutoCurry
This file contains hidden or 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 toArray(arr, start){ return Array.prototype.slice.call(arr, start || 0) } | |
Function.prototype.curry = function(){ | |
var __method = this, args = toArray(arguments); | |
return function(){ | |
return __method.apply(this, args.concat(toArray(arguments))); | |
} | |
} | |
Function.prototype.autoCurry = function(numArgs){ | |
var __method = this; | |
numArgs = numArgs || __method.length; | |
return function autoCurried(){ | |
var args = toArray(arguments) | |
, remaining = numArgs - args.length | |
;//var | |
return remaining > 0 ? | |
__method.curry.apply(__method, args).autoCurry(remaining) : | |
__method.apply(__method, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment