Skip to content

Instantly share code, notes, and snippets.

@hikari-no-yume
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hikari-no-yume/2adb5a8f8c2acea124e3 to your computer and use it in GitHub Desktop.
Save hikari-no-yume/2adb5a8f8c2acea124e3 to your computer and use it in GitHub Desktop.
Tiny function and operator curriers
function curry(func, arity) {
arity = (arity === undefined) ? func.length : arity;
if (arity > 1) {
return function (arg) {
var args = Array.prototype.slice.call(arguments, 0);
return function partial (arg) {
args = args.concat(Array.prototype.slice.call(arguments, 0));
if (args.length < arity) {
return partial;
} else {
return func.apply(this, args);
}
};
};
} else {
return func;
}
}
function operatorFunction(isLeftArg, operator, isRightArg) {
var parts = [];
if (isLeftArg) {
parts.push('leftArg');
}
if (isRightArg) {
parts.push('rightArg');
}
parts.push('return (' + (isLeftArg ? 'leftArg ' : '') + operator + (isRightArg ? ' rightArg' : '') + ');');
return Function.apply(Object.create(Function.prototype), parts);
}
function curryOperator(isLeftArg, operator, isRightArg) {
return curry(curryOperator(isLeftArg, operator, isRightArg), isLeftArg + isRightArg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment