Function Currying in JavaScript

Function currying in JavaScript is a technique where a function that takes multiple arguments is transformed into a sequence of functions, each taking a single argument. This allows you to partially apply the function, creating new functions with some of the arguments pre-filled.
Here’s a basic example of currying in JavaScript:

Example 1: Manual Currying


function add(a) {
  return function(b) {
    return a + b;
  };
}

const add5 = add(5); // Partially apply the function with `a = 5`
console.log(add5(3)); // Output: 8 (5 + 3)

In this example:

  1. The add function takes one argument a and returns another function.

  2. The returned function takes the second argument b and returns the sum of a and b.

  3. add5 is a new function created by partially applying add with a = 5.

Example 2: Generalized Currying Function

You can create a utility function to curry any function dynamically:

function curry(fn) {
  return function curried(...args) {
    if (args.length >= fn.length) {
      return fn.apply(this, args);
    } else {
      return function(...moreArgs) {
        return curried.apply(this, args.concat(moreArgs));
      };
    }
  };
}

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

const curriedSum = curry(sum);
console.log(curriedSum(1)(2)(3)); // Output: 6
console.log(curriedSum(1, 2)(3)); // Output: 6
console.log(curriedSum(1)(2, 3)); // Output: 6

In this example:

  1. The curry function takes a function fn and returns a curried version of it.

  2. The curried function checks if the number of arguments passed (args.length) is greater than or equal to the number of parameters expected by fn (fn.length).

  3. If enough arguments are provided, it calls the original function fn with those arguments.

  4. If not, it returns a new function that waits for the remaining arguments.

Example 3: Currying with Arrow Functions (ES6)

You can also use arrow functions to make currying more concise:


const add = a => b => a + b;

const add5 = add(5);
console.log(add5(3)); // Output: 8

Use Cases for Currying

  1. Partial Application: Pre-fill some arguments to create specialized functions.

  2. Function Composition: Combine smaller functions to build more complex ones.

  3. Reusability: Create reusable utility functions with fixed parameters.

Example 4: Real-World Use Case


// Curried function to calculate discounts
const calculateDiscount = discount => price => price * (1 - discount);

const tenPercentDiscount = calculateDiscount(0.1);
const twentyPercentDiscount = calculateDiscount(0.2);

console.log(tenPercentDiscount(100)); // Output: 90
console.log(twentyPercentDiscount(100)); // Output: 80

In this example:

  • calculateDiscount is a curried function that takes a discount and returns a function to apply that discount to a price.

  • tenPercentDiscount and twentyPercentDiscount are specialized functions created by partially applying calculateDiscount.

Currying is a powerful functional programming technique that can make your code more modular and expressive.
An unhandled error has occurred. Reload 🗙