Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.curryRight2() method is used to return a curried version of the given function where a maximum of two arguments are processed from right to left.
Syntax:
_.curryRight2( fun )
Parameters: This method takes a single parameter as listed above and discussed below:
- fun: This is the function that should be used in the curried version.
Return Value: This method returns a curried function.
Note: This method will not work in normal JavaScript because it requires the Lodash contrib library to be installed. The lodash-contrib library can be installed using npm install lodash-contrib –save
Example 1:
// Defining lodash contrib variable var _ = require( 'lodash-contrib' );
// Function to curry function div(a, b) {
return a / b;
} // Using the _.curryRight2() method var curried = _.curryRight2(div);
console.log( "Curried division is :" ,
curried(4)(32));
|
Output:
Curried division is : 8
Example 2:
// Defining lodash contrib variable var _ = require( 'lodash-contrib' );
// Function to curry function div(a, b) {
return a / b;
} // Using the _.curryRight2() method var curried = _.curryRight2(div);
console.log( "Curried division is :" ,
curried(32)(4));
|
Output:
Curried division is : 0.125
Example 3:
// Defining lodash contrib variable var _ = require( 'lodash-contrib' );
// Function to curry function sub(a, b) {
return a - b;
} // Using the _.curryRight2() method var curried = _.curryRight2(sub);
console.log( "Curried Subtraction is :" ,
curried(2)(10));
|
Output:
Curried Subtraction is : 8
Example 4:
// Defining lodash contrib variable var _ = require( 'lodash-contrib' );
// Function to curry function div(a, b) {
return ( "a=" + a + " and b=" + b);
} // Using the _.curryRight2() method var curried = _.curryRight2(div);
console.log(curried( "a" )( "b" ));
|
Output:
a=b and b=a