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.

Revisions

  1. Nick created this gist Jun 30, 2014.
    27 changes: 27 additions & 0 deletions curry.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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));