[Javascript] Currying

Currying is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c).

// curry(f) does the currying 
function curry(f) {transform
  return function(a) {
    return function(b) {
      return f(a, b);
    };
  };
}

// usage
function sum(a, b) {
  return a + b;
}

let curriedSum = curry(sum);

alert( curriedSum(1)(2) ); // 3

Benefits

It reduces function call When you need to use the same function calls a lot.

좋은 웹페이지 즐겨찾기