Currying in JavaScript

Currying in JavaScript

Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions. It returns a new function that expects the next argument inline. In other words, when a function, instead of taking all arguments at one time, takes the first one and return a new function that takes the second one and returns a new function which takes the third one, and so forth, until all arguments have been fulfilled.

Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions. It returns a new function that expects the next argument inline.

In other words, when a function, instead of taking all arguments at one time, takes the first one and return a new function that takes the second one and returns a new function which takes the third one, and so forth, until all arguments have been fulfilled.

That is, when we turn a function call sum(1,2,3) into sum(1)(2)(3)

The number of arguments a function takes is also called arity.

function sum(a, b) {
    // do something
}
function _sum(a, b, c) {
    // do something
}

function sum takes two arguments (2-arity function) and _sum takes three arguments (3-arity function).

Curried functions are constructed by chaining closures by defining and immediately returning their inner functions simultaneously.

Why it’s useful ?

  1. Currying helps we avoid passing the same variable again and again.
  2. It helps to create a higher order function

Currying transforms a function with multiple arguments into a sequence/series of functions each taking a single argument.

Example:

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

sum(1,2,3); // 6

As we see, function with the full arguments. Let’s create a curried version of the function and see how we would call the same function (and get the same result) in a series of calls:

function sum(a) {
    return (b) => {
        return (c) => {
            return a + b + c
        }
    }
}
console.log(sum(1)(2)(3)) // 6

We could separate this sum(1)(2)(3) to understand it better:

const sum1 = sum(1);
const sum2 = sum1(2);
const result = sum2(3);
console.log(result); // 6

Let’s get to know how it works:

We passed 1 to the sum function:

let sum1 = sum(1);

It returns the function:

return (b) => {
        return (c) => {
            return a + b + c
        }
}

functional-programming web-development javascript currying front-end-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Front-end Developer | Dedicated Front-end Programmers In India

Hire Front-end web developers and programmers in India from ValueCoders for your custom offshore Front-end application development projects. Hire Front-end developer from ValueCoders and get interactive Front-end web designs. 16+ years exp. , 100% money back guarantee, 450+ Staff . Contact Us Today!

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

Hire dedicated JavaScript Developers who are proficient in AngularJS, ReactJS, NodeJS, & VueJS frameworks. Get flexible hiring models as per your business requirements.

filterMap in JavaScript, efficiency for functional programming

All JavaScript developers are awesome and they deserve a brilliant way of filtering and mapping over an array with just one function with a linear complexity O(n) of one run through the array.

Top 19 Front-End Web Development Tools to Consider in 2020

In this post, we are putting curated list of top tools with key features and download links "Top 19 Front End Web Development Tools to Consider in 2020"

5 JavaScript Projects You Should Build As a Front-End Developer

5 JavaScript Projects You Should Build As a Front-End Developer: Brochure website; A blog template with HTML and CSS; Web application using an API; An eCommerce shopping cart; Personal portfolio