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 from Right Example:
4.2.
JavaScript
4.3.
Currying with Data Transformations:
4.4.
JavaScript
4.5.
Event Handler with Curried Function:
4.6.
JavaScript
4.7.
Combining Functions in Right-to-Left Order:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
What is the difference between _.curry() and _.curryRight()?
5.2.
How does _.curryRight() handle extra arguments?
5.3.
In what scenarios is _.curryRight() particularly useful?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.curryRight() Method

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

Introduction

Currying is a fundamental concept in functional programming, and Lodash enhances this capability with the _.curryRight() method. Similar to _.curry(), this function transforms a multi-argument function into a sequence of functions, but with a right-to-left execution order. _.curryRight() allows for the partial application of arguments from right to left, offering an alternative approach to function composition. 

Lodash _.curryRight() Method

This article will explore the _.curryRight() method, detailing its syntax, use cases, and advantages, supported by examples and FAQs.

Why This Function is Used

The _.curryRight() function is used when there's a need to apply function arguments in a right-to-left manner. This is particularly useful in scenarios where the last arguments of a function are known beforehand, or when functions are composed in a way that the output of one function feeds as the rightmost argument to another. It allows for a more flexible and intuitive way of partially applying arguments, especially in complex functional transformations.

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

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

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

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

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 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