JavaScript: What is Currying ?

Currying is an advanced technique of working with functions. It’s used not only in JavaScript, but in other languages as well.

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

Currying doesn’t call a function. It just transforms it.

Let’s see an example first, to better understand what we’re talking about, and then practical applications.

We’ll create a helper function curry(f) that performs currying for a two-argument f. In other words, curry(f) for two-argument f(a, b) translates it into a function that runs as f(a)(b):

function curry(f) {

// curry(f) does the currying 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

More advanced implementations of currying, such as _.curry from lodash library, return a wrapper that allows a function to be called both normally and partially:
 

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

let curriedSum = _.curry(sum); // using _.curry from lodash library

alert( curriedSum(1, 2) ); // 3, still callable normally
alert( curriedSum(1)(2) ); // 3, called partially
Advanced curry implementation

In case you’d like to get in to the details, here’s the “advanced” curry implementation for multi-argument functions that we could use above.It’s pretty short:
 

function curry(func) {

  return function curried(...args) {
    if (args.length >= func.length) {
      return func.apply(this, args);
    } else {
      return function(...args2) {
        return curried.apply(this, args.concat(args2));
      }
    }
  };
}

Usage examples:

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

let curriedSum = curry(sum);

alert( curriedSum(1, 2, 3) ); // 6, still callable normally
alert( curriedSum(1)(2,3) ); // 6, currying of 1st arg
alert( curriedSum(1)(2)(3) ); // 6, full currying
Summary

Currying is a transform that makes f(a,b,c) callable as f(a)(b)(c). JavaScript implementations usually both keep the function callable normally and return the partial if the arguments count is not enough.

Currying allows us to easily get partials. As we’ve seen in the logging example, after currying the three argument universal function log(date, importance, message) gives us partials when called with one argument (like log(date)) or two arguments (like log(date, importance)).

Readmore: https://javascript.info/currying-partials#advanced-curry-implementation

callable

@callable
* tính từ
- có thể gọi được
- có thể trả ngay được (tiền nợ)

curry

@curry /'kʌri/
* danh từ
- bột ca ri
- món ca ri
* ngoại động từ
- nấu ca ri, cho bột ca ri (vào đồ ăn)
=curried chicken+ món gà nấu ca ri
* ngoại động từ
- chải lông (cho ngựa)
- sang sưa (da thuộc)
- đánh đập, hành hạ (ai)
!to curry favour with somebody
- nịnh hót ai, bợ đỡ ai, xun xoe với ai để cầu ân huệ, cầu cạnh ai để xin ân huệ

implementation

@implementation /,implimen'teiʃn/
* danh từ
- sự thi hành, sự thực hiện đây đủ
=the implementation of an agreement+ sự thi hành một hiệp định
- sự bổ sung

partially

@partially
* phó từ
- không hoàn chỉnh; cục bộ, một phần
- một cách không vô tư; thiên vị

wrapper

@wrapper /'ræpə/
* danh từ
- tờ bọc (sách); băng (tờ báo); lá áo (điếu xì gà)
- người bao gói; giấy gói, vải gói
- áo choàng đàn bà (mặc trong nhà)