Categories
Uncategorized

Currying

Breaking down a function into a series of function calls with an argument. The following is an Javascript ES6 example of a sum function that returns the sum of the calls until no argument is passed.

Code

const add = (num) => {
  if (!num)
    return 0;
  let sum = 0 + num;
  const ret = (x) => {
    if (!x) {
      return sum;
    } else {
      sum += x;
      return ret;
    }
  };
  return ret;
};

alert(add(5)(6)(7)());

Demo