Lodash _.curryRight2() Method

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:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));

chevron_right


Output:

Curried division is : 8

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));

chevron_right


Output: 

Curried division is : 0.125

Example 3:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));

chevron_right


Output: 

Curried Subtraction is : 8

Example 4:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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"));

chevron_right


Output: 

a=b and b=a

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.