function fn() {
    // implementation
}
fn(2,3,4); // 24
fn(2,3)(4); // 24
fn(2)(3)(4); // 24
I am not able to achieve the multiplication in the example shown above. How am I able to achieve it? Any kind of help is highly appreciated!
This is called a JavaScript closure. It makes it possible for a function to have "private" variables. The counter is protected by the scope of the anonymous function, and can only be changed using the add function. A closure is a function having access to the parent scope, even after the parent function has closed.
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.
The multiplication operator ( * ) multiplies numbers.
Explanation: In JavaScript, closures are created every time a function is created, at function creation time. Technically, all JavaScript functions are closures: they are objects, and they have a scope chain associated with them. 5. Which of the following uses a lot of CPU cycles?
You could have a function that returns itself with bound arguments if the total number of arguments provided so far is less than 3:
const multiply = (...args) => (
  args.length < 3
  ? multiply.bind(null, ...args)
  : args.reduce((a, b) => a * b)
);
console.log(multiply(2, 3, 4));
console.log(multiply(2, 3)(4));
console.log(multiply(2)(3)(4));
console.log(multiply(2)(3, 4));
    function curry(func,args,space) {
        var n  = func.length - args.length; //arguments still to come
        var sa = Array.prototype.slice.apply(args); // saved accumulator array
        function accumulator(moreArgs,sa,n) {
            var saPrev = sa.slice(0); // to reset
            var nPrev  = n; // to reset
            for(var i=0;i<moreArgs.length;i++,n--) {
                sa[sa.length] = moreArgs[i];
            }
            if ((n-moreArgs.length)<=0) {
                var res = func.apply(space,sa);
                // reset vars, so curried function can be applied to new params.
                sa = saPrev;
                n  = nPrev;
                return res;
            } else {
                return function (){
                    // arguments are params, so closure business is avoided.
                    return accumulator(arguments,sa.slice(0),n);
                }
            }
        }
        return accumulator([],sa,n);
    }
    // now you can define any type of function, add, Mul etc...
    function add (a,b,c){
          if (arguments.length < this.add.length) {
            return curry(this.add,arguments,this);
          }
          return a+b+c;
    }
    function mul (a,b,c){
        if (arguments.length < this.mul.length) {
          return curry(this.mul,arguments,this);
        }
        return a*b*c;
    }
    // Multiplication
    console.log("Multiplication Sample");
    console.log(mul(2,3,4))         // 24
    console.log(mul(2,3)(4))        // 24
    console.log(mul(2)(3)(4))       // 24
    console.log(mul()(1,2,4));      // 8
    console.log(mul(1)(2)(5));      // 10
    console.log(mul(1)()(2)()(6));  // 12
    // Addition
    console.log("Addition Sample");
    console.log(add(2,3,4))         // 9
    console.log(add(2,3)(4))        // 9
    console.log(add(2)(3)(4))       // 9
    console.log(add()(1,2,4));      // 7
    console.log(add(1)(2)(5));      // 8
    console.log(add(1)()(2)()(6));  // 9
Reference : http://www.crockford.com/javascript/www_svendtofte_com/code/curried_javascript/index.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With