Skip to content

Instantly share code, notes, and snippets.

@gtzilla gtzilla/functools.js forked from jsocol/functools.js
Created Feb 12, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.