-
-
Save gtzilla/823629 to your computer and use it in GitHub Desktop.
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 partial = exports.partial = function partial(fn) { | |
var args = arguments, | |
func = fn; | |
[].shift.apply(args); | |
return function() { | |
var _args = args; | |
[].push.apply(_args, arguments); | |
return func.apply(func, _args); | |
}; | |
}; | |
var curry = exports.curry = function curry(fn, x) { | |
var m = /\(([^\)]+)\)/.exec(fn.toString()), | |
n = m[1].split(',').length, | |
func = fn; | |
if (func.num_args != undefined) | |
n = func.num_args; | |
else | |
func.num_args = n; | |
if (1 == n) { | |
if (x != undefined) | |
return func(x); | |
return func; | |
} else if (n > 1) { | |
if (x == undefined) { | |
var _func = function(y) { | |
return curry(func, y); | |
}; | |
_func.num_args = n - 1; | |
return _func; | |
} else { | |
var _func = function(y) { | |
var _p = partial(func, x); | |
_p.num_args = n - 1; | |
return curry(_p, y); | |
}; | |
_func.num_args = n - 1; | |
return _func; | |
} | |
} else { | |
throw "Must be called on a function that takes arguments."; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment