Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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.
Reverse Function Composition:
4.2.
JavaScript
4.3.
Text Processing in Reverse Order:
4.4.
JavaScript
4.5.
Data Transformation Pipeline:
4.6.
JavaScript
4.7.
Combining Multiple Operations:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
What is the difference between _.flow() and _.flowRight()?
5.2.
Can _.flowRight() handle asynchronous functions?
5.3.
Is there a limit to the number of functions that can be composed with _.flowRight()?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.flowRight() Method

Author Pallavi singh
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

In functional programming, the composition of functions is a key concept, where the output of one function becomes the input of another. Lodash's _.flowRight() method (also known as _.compose()) offers a way to achieve this by creating a new function that composes multiple functions from right to left. 

Lodash _.flowRight() Method

This method is particularly useful in scenarios where you need to process data through a series of transformations in a specific order.

Why This Function is Used

The _.flowRight() function is used for composing functions in reverse order compared to _.flow(). While _.flow() applies functions from left to right, _.flowRight() applies them from right to left. This is crucial in situations where the order of function application matters and where you need the last function in the argument list to be applied first.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax: 

_.flowRight([funcs])

Parameters:

[funcs] (...Function): The functions to invoke, applied from right to left.

Return Value: 

(Function) - Returns the new composite function.

Examples 

Reverse Function Composition:

  • JavaScript

JavaScript

var _ = require('lodash');

var addFive = n => n + 5;

var square = n => n * n;

var compute = _.flowRight([square, addFive]);

console.log(compute(5));

Output: 

100 (square(5 + 5))


Demonstrates composing functions in reverse order, where addFive is applied before square.

Text Processing in Reverse Order:

  • JavaScript

JavaScript

var exclaim = text => text + '!';

var toUpper = text => text.toUpperCase();

var shout = _.flowRight([toUpper, exclaim]);

console.log(shout('hello'));

Output:

 'HELLO!'


Shows creating a string processing function where exclamation is added before converting to uppercase.

Data Transformation Pipeline:

  • JavaScript

JavaScript

var wrapInDiv = text => `<div>${text}</div>`;

var makeBold = text => `<b>${text}</b>`;

var htmlFormatter = _.flowRight([wrapInDiv, makeBold]);

console.log(htmlFormatter('Hello'));

Output:

 '<div><b>Hello</b></div>'


An example of a data transformation pipeline for HTML formatting, applied in reverse order.

Combining Multiple Operations:

  • JavaScript

JavaScript

var increment = n => n + 1;

var double = n => n * 2;

var negate = n => -n;

var complexOperation = _.flowRight([negate, double, increment]);

console.log(complexOperation(3));

Output:

 -8 (-(3 + 1) * 2)


Demonstrates combining multiple arithmetic operations in reverse order.

Frequently Asked Questions

What is the difference between _.flow() and _.flowRight()?

The primary difference is the order in which functions are applied. _.flow() applies functions from left to right, whereas _.flowRight() applies them from right to left.

Can _.flowRight() handle asynchronous functions?

Like _.flow(), _.flowRight() is designed for synchronous functions. Asynchronous functions would require handling with Promises or async/await.

Is there a limit to the number of functions that can be composed with _.flowRight()?

There's no specific limit, but for readability and maintainability, it's advisable to keep the number of composed functions reasonable.

Conclusion

Lodash's _.flowRight() method, or _.compose(), is a powerful tool for creating composite functions that apply operations in reverse order. It's particularly useful for building readable and maintainable data processing pipelines and adhering to functional programming principles.

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.

Previous article
Lodash _.findLastKey() Method
Next article
Lodash _.matchesProperty() Method
Live masterclass