Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Basic Currying Example:
4.2.
JavaScript
4.3.
Currying with Placeholder:
4.4.
JavaScript
4.5.
Event Handler with Curried Function:
4.6.
JavaScript
4.7.
Currying a Function with Multiple Arguments:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
What are the advantages of using _.curry() in functional programming?
5.2.
How does _.curry() handle extra arguments?
5.3.
Can curried functions be used with other Lodash utilities?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.curry() Method

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In functional programming, currying is a powerful technique that transforms a function with multiple arguments into a sequence of functions, each taking a single argument. Lodash enhances JavaScript's capabilities in this area with the _.curry() method. This function creates a new function that can be invoked with fewer arguments than the original function and returns a new function awaiting the remaining arguments. 

Lodash _.curry() Method

This article will explore the _.curry() method, focusing on its syntax, applications, and benefits, along with practical examples and FAQs.

Why This Function is Used

The _.curry() function is used to simplify functions that take multiple arguments into a chain of functions, each accepting a single argument. This is particularly useful for creating more modular, reusable, and easier-to-understand code. It allows for partial application of function arguments, enabling the gradual application of a function over time or in various contexts.

Syntax, Parameter and Return Value

Syntax: 

_.curry(func, [arity=func.length])

Parameters:

  • func (Function): The function to curry.
     
  • [arity=func.length] (number): The arity of func.

Return Value: 

(Function) - Returns the new curried function.

Examples 

Basic Currying Example:

  • JavaScript

JavaScript

var _ = require('lodash');

function add(a, b, c) {

 return a + b + c;

}

var curriedAdd = _.curry(add);

console.log(curriedAdd(1)(2)(3));

console.log(curriedAdd(1, 2)(3));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

6
6


Demonstrates a simple curried function for addition.

Currying with Placeholder:

  • JavaScript

JavaScript

var curriedAddWithPlaceholder = curriedAdd(1, _, 3);

console.log(curriedAddWithPlaceholder(2));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

6

Shows using a placeholder to specify partial arguments.

Event Handler with Curried Function:

  • JavaScript

JavaScript

function logEvent(eventName, event) {

 console.log(eventName + ':', event.type);

}

var curriedLogger = _.curry(logEvent);

document.addEventListener('click', curriedLogger('click'));
You can also try this code with Online Javascript Compiler
Run Code

// On a click event, logs: 'click: click'

An example of using a curried function as an event handler.

Currying a Function with Multiple Arguments:

  • JavaScript

JavaScript

function multiply(x, y, z) {

 return x * y * z;

}

var curriedMultiply = _.curry(multiply);

console.log(curriedMultiply(2)(3)(4));

console.log(curriedMultiply(2, 3)(4));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

24
24


Demonstrates a curried function that multiplies three numbers.

Frequently Asked Questions 

What are the advantages of using _.curry() in functional programming?

_.curry() enhances function reusability, modularity, and readability by breaking down complex functions into simpler chained functions, each taking a single argument.

How does _.curry() handle extra arguments?

A curried function, after receiving its expected number of arguments, will execute with those arguments. Extra arguments passed to the last function call are ignored.

Can curried functions be used with other Lodash utilities?

Yes, curried functions can be combined with other Lodash utilities like _.compose() or _.flow() to create more complex functional constructs.

Conclusion

Lodash's _.curry() method provides an elegant way to apply the principles of currying in JavaScript, allowing for partial application of arguments and creation of highly modular and reusable functions. It's a valuable tool in the functional programmer's toolkit, enabling more expressive and maintainable code.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass