Search code examples
javascriptfunctional-programmingecmascript-6applycurrying

Function application for curried functions in JavaScript and ES6


I love that ECMAScript 6 allows you to write curried functions like this:

var add = x => y => z => x + y + z;

However, I hate that we need to parenthesize every argument of a curried function:

add(2)(3)(5);

I want to be able to apply curried functions to multiple arguments at once:

add(2, 3, 5);

What should I do? I don't care about performance.


Solution

  • Currying and the application of curried functions are controversial issues in Javascript. In simple terms, there are two opposing views, which I illustrate both briefly.


    - Use of a separate curry function only when necessary

    The adaptation of concepts from other languages or paradigms is in principle a good thing. This adaptation though, should be done with the elementary means of the target language. What does that mean for currying in javascript?

    • curried functions are called as a sequence of unary functions:add3(1)(2)(3); // 6
    • own functions are manually curried with arrows const add3 = x => y => z => x + y + z;
    • third party functions or methods are curried by a separate curry function

    - Use of a separate curry implementation by default

    There's a problem with the proposed $/uncurry function:

    const $ = (func, ...args) => args.reduce((f, x) => f(x), func);
    const sum = x => y => z => x + y + z;
    
    $(sum, 1, 2, 3); // 6
    $(sum, 1, 2)(3); // 6
    $(sum, 1)(2, 3); // z => x + y + z
    

    In this way uncurried functions can only once be applied with an unlimited number of arguments. Any subsequent calls must be made unary. The function does exactly what it promises. However, it does not allow application of curried functions, such as JavaScript developers are used to. Most of the current curry implementations are more flexible. Here's an extended implementation:

    const uncurry = f => (...args) => args.reduce(
      (g, x) => (g = g(x), typeof g === "function" && g.length === 1
       ? uncurry(g) 
       : g), f
    );
    
    const sum = uncurry(x => y => z => x + y + z);
    
    sum(1, 2, 3); // 6
    sum(1, 2)(3); // 6
    sum(1)(2, 3); // 6
    

    This implementation works, if you like auto-uncurrying: Once a uncurried function itself produces a curried function as a return value, this returned function is automatically uncurried. If you prefer more control, the following implementation might be more appropriate.

    The final uncurry implementation

    const partial = arity => f => function _(...args) {
      return args.length < arity
       ? (...args_) => _(...args.concat(args_))
       : f(args);
    };
    
    const uncurry = arity => f => partial(arity)(args => args.reduce((g, x) => g(x), f));
    const sum = uncurry(3)(x => y => z => x + y + z);
    
    sum(1, 2, 3); // 6
    sum(1, 2)(3); // 6
    sum(1)(2, 3); // 6
    

    This tiny arity parameter brings us the desired control. I think it's worth it.

    A curry solution for the rest

    What do we do with functions that are beyond our control and hence haven't been manually curried?

    const curryN = uncurry(2)(arity => f => partial(arity)(args => f(...args)));
    const add = curryN(2, (x, y) => x + y);
    const add2 = add(2);
    
    add2(4); // 6
    

    Fortunately, we were able to reuse partial and keep curryN concise. With this solution also variadic functions or such with optional parameters can be curried.

    Bonus: "Funcualizing" and currying Methods

    To curry methods, we need to transform this nasty, implicit this property in an explicit parameter. It turns out that we can reuse partial for an adequate implementation once again:

    const apply = uncurry(2)(arity => key => {
      return arity
       ? partial(arity + 1)(args => args[arity][key](...args.slice(0, arity)))
       : o => o[key]();
    });
    
    apply(0, "toLowerCase")("A|B|C"); // "a|b|c"
    apply(0, "toLowerCase", "A|B|C"); // "a|b|c"
    
    apply(1, "split")("|")("A|B|C"); // ["A", "B", "C"]
    apply(1, "split")("|", "A|B|C"); // ["A", "B", "C"]
    apply(1, "split", "|", "A|B|C"); // ["A", "B", "C"]
    
    apply(2, "includes")("A")(0)("A|B|C"); // true
    apply(2, "includes", "A", 0, "A|B|C"); // true
    

    In this blog post currying is discussed in detail.