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' }