Skip to content

Instantly share code, notes, and snippets.

@finalprototype
Created June 30, 2014 04:53
Show Gist options
  • Save finalprototype/eed4427fc9cadc880612 to your computer and use it in GitHub Desktop.
Save finalprototype/eed4427fc9cadc880612 to your computer and use it in GitHub Desktop.
Expected argument length curry
function add (a, b) {
return a + b;
}
function multiply (a, b) {
return a * b;
}
function applyFn (fn) {
var parentArgs = Array.prototype.slice.call(arguments, 0);
function wrapper () {
var args = parentArgs.slice(1);
args = args.concat(Array.prototype.slice.call(arguments, 0));
if (args.length >= 2) {
return fn.apply(this, args);
}
return applyFn.apply(this, parentArgs.concat(args));
}
return arguments.length-1 >= 2 ? wrapper() : wrapper;
}
var f = applyFn(multiply);
console.log('3x5', f(3)(5));
console.log('2x8', f(2)(8));
f = applyFn(add);
console.log('3+5', f(3)(5));
console.log('2+8', f(2)(8));
console.log('2+8', f(2)(8)(10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment