[Javascript] Currying

Suyeon·2020년 10월 20일
0

Javascript

목록 보기
22/31

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.

profile
Hello World.

0개의 댓글