Javascript – Have they missunderstood currying or have I

curryingjavascript

This question is similar to the question posted on Does groovy call partial application 'currying'?, but not completely the same, and the answers given there do not really satisfy me.

I would like to state right at this point, before I go any further, that currying is one of the concepts that have been hard to fully understand. I think the reason for that is the fact that there seem to be two general definitions of currying, and they don't really mean the same thing both in theory and in application.

That being said, I think currying is a very simple concept once you decide to take only one of the two definitions and stick to it. In fact, currying should not be difficult to understand at all, but in my experience, understanding currying has been the equivalent of showing a little boy an apple and telling him that fruit is called apple, and then you go on calling that fruit an orange. The boy will never understand the simple concept of an apple because you have caused an enormous amount of confusion by calling the apple an orange.

The definition of currying that I've accepted as the right one is that currying is taking a function of an arbitrary arity and turning it into multiple chainable functions each of which accepts only one parameter (unary functions). In code, it would look like this:

f(1,2,3);
//becomes
f(1)(2)(3);

However, some people, including well known programmers like John Resig, explain currying as something completely different. For them, currying is no more than simple partial application. There is even a generally accepted javascript function for currying:

Function.prototype.curry = function() {
    var fn = this, args = Array.prototype.slice.call(arguments);
    return function() {
      return fn.apply(this, args.concat(
        Array.prototype.slice.call(arguments)));
    };
  };

This functions does not really curry anything, but rather partially applies the supplied arguments. The reason why I don't think this function curries anything is because you can end up with a function that accepts more than one argument:

var f2 = f.curry(1); //f accepts 3 arguments f(1,2,3);
f2(2,3); // calling the resulting "curried" function.

So, my question is, have I misunderstood currying or have they?

I will leave you with this post that explains currying in javascript, but in a way in which I don't really see any currying: https://javascriptweblog.wordpress.com/2010/04/05/curry-cooking-up-tastier-functions/

Best Answer

You are right in saying that what many (if not most) people in the JavaScript community refer to as currying does not really correspond to the term as defined in Haskell.

Then again, neither does partial application. A partial application of all arguments is a full application, but this in not happening here, because if you do this for all arguments, you will still not have applied the function, but rather created a niladic function from it (a distinction that makes no sense in Haskell).

At the bottom line, for a JavaScript programmer it's more important to understand what other JavaScript programmers mean by "currying" or "partial application", than what a non-JavaScript programmer use the term for.

FIY, I think the correct (and not unheard of) term here is binding. You bind the arguments of a function. In particular, you can also bind the implicit this argument of a function.

Related Topic