Skip to content

Instantly share code, notes, and snippets.

@jsocol
Created February 12, 2011 08:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsocol/823598 to your computer and use it in GitHub Desktop.
Save jsocol/823598 to your computer and use it in GitHub Desktop.
partial and curry
var partial = exports.partial = function partial(fn) {
var args = arguments,
func = fn;
[].shift.apply(args);
return function() {
var _args = [].slice.call(args, 0);
[].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.";
}
};
@jsocol
Copy link
Author

jsocol commented Feb 12, 2011

function wxyz (w, x, y, z) {
    return w + x * y + z;
}
wxyz(1, 2, 3, 4); // 11
f = functools.curry(wxyz);
f(1)(2)(3)(4); // 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment