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
var _ = require('lodash');
var addFive = n => n + 5;
var square = n => n * n;
var compute = _.flowRight([square, addFive]);
console.log(compute(5));

You can also try this code with Online Javascript Compiler
Run Code
Output:
100 (square(5 + 5))
Demonstrates composing functions in reverse order, where addFive is applied before square.
Text Processing in Reverse Order:
JavaScript
var exclaim = text => text + '!';
var toUpper = text => text.toUpperCase();
var shout = _.flowRight([toUpper, exclaim]);
console.log(shout('hello'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'HELLO!'
Shows creating a string processing function where exclamation is added before converting to uppercase.
Data Transformation Pipeline:
JavaScript
var wrapInDiv = text => `<div>${text}</div>`;
var makeBold = text => `<b>${text}</b>`;
var htmlFormatter = _.flowRight([wrapInDiv, makeBold]);
console.log(htmlFormatter('Hello'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'<div><b>Hello</b></div>'
An example of a data transformation pipeline for HTML formatting, applied in reverse order.
Combining Multiple Operations:
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));

You can also try this code with Online Javascript Compiler
Run Code
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 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.