Currying is a transform that makes f(a,b,c) callable as f(a)(b)(c) .

Introduction

We want to specialize a JavaScript function of fixed arity by transforming it first into a composition of functions of lower arities.

First, consider a function of the following form:

const fn = (p1, p2, ..., pN) => f(p1, p2, ..., pN);

In its simplest form, this problem consists of transforming this function into a new function of the following form:

const curried = p1 => (p2, ..., pN) => fn(p1, p2, ..., pN);

We can also generalize this process to get the following form:

const curried = p1 => p2 => ... => pN => fn(p1, p2, ..., pN);

This last transformation is also called currying after the American mathematician Haskell Curry whose work laid the foundations of functional programming.

In this article, we propose to study different aspects of currying in JavaScript. In particular, we will see:

  • a simple example to understand what currying is in practice.
  • several JavaScript implementations allowing to carry out the transformations above.
  • a generic and flexible variant of transforming a JavaScript function into a composition of functions of variable arity.
  • an example illustrating the limits of the proposed implementations with regard to the currying of functions of variable arity.

A simple example of currying

We want to compute the price all taxes included (ATI) of products belonging to different categories using a function ATI:

const ATI = (rate, price) => (1 + rate) * price;const price_tv = ATI(0.2, 299);
const price_apples = ATI(0.055, 1.99);
const price_tomatoes = ATI(0.055, 2.99);

Now let’s rewrite our function ATI in its curried form:

const ATI = rate => price => (1 + rate) * price;

This allows us in particular to define functions for computing the price all taxes included by category:

const ATI_service = ATI(0.2);
const ATI_first_necessity = ATI(0.055);

We can thus rewrite our price computations including taxes without repeating the value of the used rate:

const ATI_tv = ATI_service(299);
const ATI_apples = ATI_first_necessity(1.99);
const ATI_tomatoes = ATI_first_necessity(2.99);

This is a very simple example of using currying so that we can then specialize a function by setting its first parameter.

Although this is not the subject of this article, note that we can also rewrite our functions ATI_service andATI_first_necessity as follows:

const ATI_service = price => ATI(0.2, price);
const ATI_first_necessity = price => ATI(0.055, price);

The resulting functions are called partial applications. We will also say that we have projected the ATI function on the rest of the parameters which have not been fixed. The advantage of this technique is to be able to set certain parameters and return a new function of lower arity.

Even if, as the example above shows, the projection of the ATI function on theprice parameter gives a function similar to the curried function, one must not confuse currying and partial application:

  • currying makes it possible to transform a function with several parameters into a composition of unary functions.
  • a partial application allows to fix a certain number of parameters of a function and returns a function of lower arity.

In this article, we will implement a generic currying function allowing the currying process to be carried out automatically, regardless of the number of parameters of the considered function.

We would like to be able to obtain, for example, the second ATI function from the first as follows:

const ATI_curried = curry(ATI);

Currying implementations

Simple currying

Consider a function of the following form:

const fn = (p1, p2, ..., pN) => f(p1, p2, ..., pN);

We want to transform it into a new function of the following form:

const curried = p1 => (p2, ..., pN) => fn(p1, p2, ..., pN);

Solution

Here is a possible solution:

const curryOne = fn => first => (...args) => fn(first, ...args);

Here, the curryOne function takes a function fn as a parameter and returns a new function taking a parameter first. When we call the unary function thus produced, we obtain a new function having memorized the first argument passed to the parameter first, and applicable to the rest of the parameters of the function fn.

A more concise solution

Note that the curryOne function can be rewritten in the following more concise way:

const curryOne = fn => first => fn.bind(null, first);

Indeed, the Function.prototype.bind() method returns here a new function with a bound argument which will be automatically placed before the other arguments which will be passed to it, which is very convenient to achieve the expected transformation.

0
Would love your thoughts, please comment.x
()
x