Syntax, Parameter and Return Value
Syntax:
_.curryRight(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 from Right Example:
JavaScript
var _ = require('lodash');
function concatenate(a, b, c) {
return a + b + c;
}
var curriedConcat = _.curryRight(concatenate);
console.log(curriedConcat('C')('B')('A'));
console.log(curriedConcat('C')('A', 'B'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'ABC'
'ABC'
Demonstrates a simple curried function for concatenation, applying arguments from right to left.
Currying with Data Transformations:
JavaScript
function transformData(data, transformFunction) {
return transformFunction(data);
}
var curriedTransform = _.curryRight(transformData);
var toUpperCase = curriedTransform(str => str.toUpperCase());
console.log(toUpperCase('hello'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'HELLO'
Shows using _.curryRight() for data transformation functions.
Event Handler with Curried Function:
JavaScript
function logEvent(event, message) {
console.log(message + ':', event.type);
}
var curriedLogger = _.curryRight(logEvent);
document.addEventListener('click', curriedLogger('Click event occurred'));

You can also try this code with Online Javascript Compiler
Run Code
// On a click event, logs: 'Click event occurred: click'
An example of using a curried function as an event handler.
Combining Functions in Right-to-Left Order:
JavaScript
var greet = name => 'Hello, ' + name;
var exclaim = statement => statement + '!';
var excitedGreeting = _.curryRight(greet)(exclaim);
console.log(excitedGreeting('Alice'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Hello, Alice!'
Demonstrates combining two functions, applying the exclaim function first, followed by the greet function.
Frequently Asked Questions
What is the difference between _.curry() and _.curryRight()?
While _.curry() applies arguments from left to right, _.curryRight() applies them from right to left, offering an alternative way of currying that starts with the rightmost arguments.
How does _.curryRight() handle extra arguments?
Similar to _.curry(), a curried function created by _.curryRight() will execute after receiving its expected number of arguments, ignoring any extra arguments passed to the final function call.
In what scenarios is _.curryRight() particularly useful?
_.curryRight() is useful in scenarios where functions need to be composed in a right-to-left order, or when the latter arguments of a function are known or fixed ahead of time.
Conclusion
Lodash's _.curryRight() method offers a powerful way to apply currying in a right-to-left order, enhancing the flexibility and expressiveness of function composition in functional programming. It's particularly useful for scenarios requiring partial application of the latter arguments or right-to-left function composition.
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.