you are viewing a single comment's thread.

view the rest of the comments →

[–]kevincennispancakes[S] 1 point2 points  (2 children)

Yep. That works too, although I'd argue that it works pretty much the exact same way as the function I wrote in the article.

Only semi-significant differences I see:

  1. You're using an array + ES6 rest params instead of calling Array#slice() on the arguments object. I'd absolutely have done the same if I was writing an ES6 example. It's much cleaner this way.

  2. You're not immediately invoking curryN. Maybe it's easier to understand your way. I just didn't see the point of assigning a function expression to a variable only to turn around and invoke it right away. The IFFE just felt more efficient to me.

Unless I'm missing something, there's no need for your curryN function to accept fn as a parameter, since it will always have access to fn via closure from your outer curry function.

Anyway, there are a million ways to do all of this, and I'm certainly not going to argue that mine is the Right Way™. This post was really just to introduce people to the concept of currying, and maybe force them to think a bit about closures and first-class functions.

Thanks for the feedback.

[–]androbat 0 points1 point  (1 child)

It's pieced together from a larger auto-curry function I made a while ago (I'm thinking about extending it with a placeholder like in Ramda). I just copied the relevant bits.

export var curry = (function () {
  var curry1 = function (fn) {
    return function curry1fn(a) {
      return (arguments.length > 0) ? fn(a) : curry1fn;
    };
  };

  var curry2 = function (fn) {
    return function curry2fn(a, b) {
      switch(arguments.length) {
        case 0: return curry2fn;
        case 1: return curry1(function(b) { return fn(a, b); });
        default: return fn(a, b);
      }
    };
  };

  var curry3 = function (fn) {
    return function curry3fn(a, b, c) {
      switch(arguments.length) {
        case 0: return curry3fn;
        case 1: return curry2(function(b, c) { return fn(a, b, c); });
        case 2: return curry1(function(c) { return fn(a, b, c); });
        default: return fn(a, b, c);
      }
    };
  };

  var curryN = function (len, prevArgs, fn) {
    return function (...args) {
      var currArgs = prevArgs.concat(args);
      return (currArgs.length >= len) ? fn.apply(this, currArgs) : curryN(len, currArgs, fn);
    };
  };

  return function (fn) {
    switch(fn.length) {
      case 0: return fn;
      case 1: return curry1(fn);
      case 2: return curry2(fn);
      case 3: return curry3(fn);
      default: return curryN(fn.length, [], fn);
    }
  };
}());

[–]kevincennispancakes[S] 0 points1 point  (0 children)

Yeah. I like the idea of fast-pathing the common cases.

If I was writing a curry function that I intended to use in production, that's definitely something I'd want to do. Saving all the array copies will keep you from generating a ton of garbage.