98.getCurry

Write a function named getCurry that receives one parameter:

  • a function that takes a variable number of arguments and returns a value - callback

The function should return a new function that we'll be able to call like in the examples below.

PS: this function is similar with the currySum one

Example 1

const sum = (...params) => {
  let total = 0;
  for (const arg of params) {
    total += arg;
  }
  return total;
}

const currySum = getCurry(sum);

console.log(currySum()); // 0
console.log(currySum(1)()); // 1
console.log(currySum(1)(2)()); // 3

Example 2

const diff = (...params) => {
  let result = params[0] ?? 0;
  for (let i = 1; i < params.length; i++) {
    result -= params[i];
  }
  return result;
}

const curryDiff = getCurry(diff);

console.log(curryDiff()); // 0
console.log(curryDiff(1)(10)()); // -9
console.log(curryDiff(100)(10)(20)(30)()); // 40