currying function - 03

Currying is a pattern that is used to transform a function containing multiple parameters into a function with a single parameter.

  function multipleParam(a, b, c) {
    return a * b * c;
  }

  console.log(multipleParam(2, 3, 5));
  // Result: 30


Transform the multiple parameters function into a single parameter function.

  function multipleParam(a) {
      return function singleParam(b) {
        return function innerFunc(c) {
          return a * b * c;
        };
      };
    }

    console.log(multipleParam(3)(2)(5));
    // Result: 30


We can also call the currying function in the following way ...

    function multipleParam(a) {
      return function singleParam(b) {
        return function innerFunc(c) {
          return a * b * c;
        };
      };
    }

    const part1 = multipleParam(3);
    const part2 = part1(2);
    const part3 = part2(5);
    console.log(part3);
    // Result: 30


Post a Comment

Previous Post Next Post