DEV Community

Discussion on: Currying in JavaScript

Collapse
 
jcubic profile image
Jakub T. Jankiewicz • Edited

Note that real curry should return functions until all arguments are added. This is curry function I use in my code:

    function curry(fn, ...init_args) {
        var len = fn.length;
        return function() {
            var args = init_args.slice();
            function call(...more_args) {
                args = args.concat(more_args);
                if (args.length >= len) {
                    return fn.apply(this, args);
                } else {
                    return call;
                }
            }
            return call.apply(this, arguments);
        };
    }
Enter fullscreen mode Exit fullscreen mode

But also this function is modification of real concept of curry that return single argument functions only. Here you can use multiple arguments at once (which is much better).

You can test the curry function at: codepen.io/jcubic/pen/LxrOYP the code in demo use ES5.