Underscore.js _.curry2() Method

The Underscore.js _.curry2() method returns a curried version of the given function but will curry exactly two arguments, no more or less.
Syntax:
_.curry2( fun )
Parameters: This method takes a single parameter as listed above and discussed below:
- fun: This is the given function passed as parameter.
Return Value: It returns a curried version of the function.
Note: To execute the below examples, you have to install the underscore-contrib library by using this command prompt and execute the following command.
npm install underscore-contrib
Example 1:
// Defining underscore contrib variable var _ = require('underscore-contrib'); // Function function fun(a, b) { return a*b; } // Making curried function var gfgFunc = _.curry2(fun); // Only operates for exactly two arguments console.log("Multiplication is :", gfgFunc(20)(23)); |
Output:
Multiplication is : 460
Example 2:
// Defining underscore contrib variable var _ = require('underscore-contrib'); // Function function fun(a, b) { return a-b; } // Making curried function var gfgFunc = _.curry2(fun); // Only operates for exactly two arguments console.log("Subtraction is :", gfgFunc(25)(23)); |
Output:
Subtraction is : 2
Example 3: Printing the arguments taken by the curry2() function.
// Defining underscore contrib variable var _ = require('underscore-contrib'); // Function function fun(x, y) { return arguments; } // Making curried function var gfgFunc = _.curry2(fun); // Only operates for exactly two arguments console.log("Curried Arguments are :", gfgFunc("a")("b")); |
Output:
Curried Arguments are : [Arguments] { '0': 'a', '1': 'b' }
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!



