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