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.
Appending Default Arguments:
4.2.
JavaScript
4.3.
Custom Logging Function:
4.4.
JavaScript
4.5.
Handling Data with Default Configuration:
4.6.
JavaScript
4.7.
Enhancing Function with Fixed Callback:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.partialRight() differ from _.partial()?
5.2.
Can _.partialRight() be used with functions of any arity?
5.3.
Is it possible to partially apply multiple arguments with _.partialRight()?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.partialRight() Method

Author Gaurav Gandhi
0 upvote

Introduction

Customizing functions by pre-setting some of their arguments can be a powerful tool in programming. Lodash's _.partialRight() method extends this capability by allowing partial application of arguments from the right. It creates a new function with specified arguments appended to those provided upon invocation. 

Lodash _.partialRight() Method

This method is especially useful when you need the flexibility of partial application but want to ensure that certain arguments are always passed last.

Why This Function is Used

The _.partialRight() function is used for creating new functions where certain arguments are pre-determined and appended after any arguments provided during the function call. This is particularly helpful in situations where the sequence of arguments is critical, and some arguments must remain consistent across different uses of the function. It enhances modularity and reusability by allowing the creation of function variants with fixed trailing arguments.

Syntax, Parameter and Return Value

Syntax:

 _.partialRight(func, [partials])

Parameters:

  • func (Function): The function to partially apply arguments to.
     
  • [partials]: The arguments to be partially applied from the right.

Return Value: 

(Function) - Returns the new partially applied function.

Examples 

Appending Default Arguments:

  • JavaScript

JavaScript

var _ = require('lodash');

function sendMessage(to, from, message) {

 console.log(`From ${from} to ${to}: ${message}`);

}

var sendMessageFromSystem = _.partialRight(sendMessage, 'System');

sendMessageFromSystem('User123', 'Hello!');
You can also try this code with Online Javascript Compiler
Run Code

 Output:

 'From System to User123: Hello!'

Demonstrates appending a default 'from' argument to a messaging function.

Custom Logging Function:

  • JavaScript

JavaScript

function log(level, time, message) {

 console.log(`[${level}] ${time}: ${message}`);

}

var errorLog = _.partialRight(log, new Date().toISOString(), 'ERROR');

errorLog('Server crashed');
You can also try this code with Online Javascript Compiler
Run Code

// Output includes current time and 'ERROR' level

Shows how to create a custom logging function with pre-set log level and timestamp.

Handling Data with Default Configuration:

  • JavaScript

JavaScript

function processData(data, config) {

 // process data with config

}

var defaultConfig = { verbose: true };

var processDataWithDefault = _.partialRight(processData, defaultConfig);

processDataWithDefault({ data: 'sample' });
You can also try this code with Online Javascript Compiler
Run Code

 

// Processes data with default configuration

An example of using _.partialRight() to process data with a default configuration object.

Enhancing Function with Fixed Callback:

  • JavaScript

JavaScript

function asyncOperation(data, callback) {

 // perform operation, then execute callback

}

var onComplete = () => console.log('Operation complete');

var asyncOperationWithCallback = _.partialRight(asyncOperation, onComplete);

asyncOperationWithCallback('data');
You can also try this code with Online Javascript Compiler
Run Code

// Performs operation and logs 'Operation complete'

Demonstrates enhancing an asynchronous operation with a fixed completion callback.

Frequently Asked Questions

How does _.partialRight() differ from _.partial()?

While _.partial() pre-fills arguments from the left (beginning), _.partialRight() pre-fills them from the right (end), appending the pre-filled arguments to those provided during the call.

Can _.partialRight() be used with functions of any arity?

Yes, _.partialRight() can be used with functions of any arity, but it's most effective when the function's trailing arguments are known and fixed.

Is it possible to partially apply multiple arguments with _.partialRight()?

Yes, you can partially apply multiple arguments using _.partialRight(). These arguments will always be appended to the end of the argument list when the new function is called.

Conclusion

Lodash's _.partialRight() method provides a versatile way to create new functions with predetermined trailing arguments. It is particularly useful in scenarios where the order of arguments is important, and some need to remain consistent across different function invocations.

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