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
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
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
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
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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.