Home » What is currying in JavaScript?
What is currying in JavaScript?

What is currying in JavaScript?

Introduction:

Currying is a programming concept that plays an important role in JavaScript, allowing developers to write modular and reusable code. In this comprehensive guide, we’ll dive into the world of curry, explore its basics, understand its principles, and demonstrate useful examples in JavaScript by the end of this blog, you’ll have a solid understanding of curry and how it can improve it your coding skills.

What is currying?

What is currying?


Currying in JavaScript is a functional programming process where a function with multiple arguments is transformed into a series of nested functions, each taking a single argument. This technique allows functions to be partially applied by creating a chain of functions that each take one argument until all arguments are fulfilled. Currying helps in creating higher-order functions, avoiding repetitive passing of the same variable, and enhancing code modularity and reusability.

Why Is Currying in JavaScript Useful?

Currying in JavaScript is useful for several reasons:

  • Creating Higher-Order Functions
  • Enhancing Code Readability and Modularity
  • Avoiding Repetitive Passing of Variables
  • Improving Error Handling
  • Reusable Code


Simple Currying implementation in JavaScript

To understand currying, let’s make a simaple example.

function add_two_val(val1, val2){
     return val1 + val2;
}
const sum1 = add_two_val(10,20);
console.log(sum); // output: 20;

The function add_two_val take two arguments, val1 and val2 , and return their sum.

As you see, this function with full arguments. Create a curried function version and demonstrate calling it in a series of calls to achieve identical results.

function add_two_sum(val1){
    return (val2) =>{
        return val1 + val2;
    }
}
let sum1 = add_two_val(10,20);
console.log(sum1);


Store the value in one variable sum1, call another function within this function with val2 as an argument, add val1 and val2 inside this function, assign the result to sum1, and finally, log the output using console.log.


Currying using ES6 syntax in JavaScript

Here’s an example of implementing currying using ES6 syntax in JavaScript:

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

// Curried version using ES6
const curriedSum = a => b => c => a + b + c;

// Usage
console.log(curriedSum(1)(2)(3)); // Output: 6
console.log(curriedSum(1)(2)(3)); // Output: 6 
console.log(curriedSum(1)(2)(3)); // Output: 6

// Can also be called partially
const addOne = curriedSum(1);
const addFour = addOne(2);
const addSeven = addFour(3);

console.log(addSeven); // Output: 6

In this example:

  1. We define a traditional sum function that takes three arguments ab, and c, and returns their sum.
  2. We then create a curried version of the sum function using arrow functions: const curriedSum = a => b => c => a + b + c;
    • This creates a series of nested functions, each taking a single argument.
    • The innermost function c => a + b + c has access to the a and b values from the outer functions through closures.
  3. We can call curriedSum by passing arguments one by one: curriedSum(1)(2)(3), which returns 6.
  4. We can also create partially applied functions by calling curriedSum with some arguments:
    • const addOne = curriedSum(1); creates a new function that adds 1 to its argument.
    • const addFour = addOne(2); creates a new function that adds 4 to its argument.
    • const addSeven = addFour(3); evaluates to 6.

This curried version using ES6 syntax is more concise and readable compared to the traditional function implementation. It leverages arrow functions and closures to create a chain of functions that each take a single argument.

What is Infinite Currying?

In JavaScript, infinite currying involves a function returning a new one with each argument passed, enabling indefinite arguments. This process enables creating a chain of functions, continuously called with more arguments, for flexible and extensible input handling.

In a function summing arbitrary arguments, infinite currying is exemplified, demonstrating its utility and elegance in sorting them.

function sum(...args) {
  const add = (...nextArgs) => sum(...args, ...nextArgs);
  add.valueOf = () => args.reduce((acc, val) => acc + val, 0);
  return add;
}

console.log(sum(1)(2)(3)(4)(5)); // Output: 15

In this example, the sum function takes any number of arguments using the rest parameter ...args. It returns a new function add that concatenates the current arguments with any new arguments passed. The valueOf method is used to calculate the sum of all arguments when the function chain is terminated.


Conclusion:

Currying is a powerful tool in application design that can have a significant impact on rule organization, readability, and reuse. JavaScript developers can use this concept to create more modular and flexible code. Understanding the principles of carrying and using real-world examples will enable you to utilize all the power of this programming example in your JavaScript projects.

More Reading

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *