The Lodash _.curry() method returns a curried version of the given function. If the reverse is true, arguments will be processed from right to left.
Syntax:
_.curry( fun, reverse )
Parameters: This method takes two parameters as listed above and discussed below.
- fun: This is the given function.
- reverse: It is an optional parameter and determines whether the arguments will be reversed or not.
Return Value: It returns a curried version of the function.
Note: To execute the below examples, you have to install the lodash-contrib library by using this command prompt and execute the following command.
npm install lodash-contrib
Example 1:Â
Javascript
// Defining lodash contrib variable var _ = require( 'lodash-contrib' );Â Â Â // Function function fun(a, b, c, d){ Â Â Â Â return a + b + c + d; } Â Â // Making curried function var gfgFunc = _.curry(fun); Â Â // Only operates for arguments same // as the number in function console.log( "Addition is :" , Â Â Â Â gfgFunc(42)(23)(20)(30)); Â Â // Not adds for less arguments console.log(gfgFunc(1)(2)(3)); |
Output:
Addition is : 115 [Function: wrapper]
Example 2:
Javascript
// Defining lodash contrib variable var _ = require( 'lodash-contrib' );Â Â Â // Function function fun(a, b, c){ Â Â Â Â return a * b * c; } Â Â // Making curried function var gfgFunc = _.curry(fun); Â Â // Only operates for arguments same // as the number in function console.log( "Multiplication is :" , Â Â Â Â gfgFunc(20)(23)(27)); Â Â // Not multiplies for less arguments console.log(gfgFunc(1)(2)); |
Output:Â
Multiplication is : 12420 [Function: wrapper]
Example 3: Arguments are processed left to right for reversed = false.
Javascript
// Defining lodash contrib variable var _ = require( 'lodash-contrib' );Â Â Â // Function function fun(a, b, c) { Â Â Â Â return arguments; } Â Â // Making curried function var gfgFunc = _.curry(fun, false ); Â Â // Only operates for arguments same // as the number in function console.log( "curried arguments are :" , Â Â Â Â gfgFunc( "arg1" , "arg2" , "arg3" )); |
Output:
curried arguments are : [Arguments] { '0': 'arg1', '1': 'arg2', '2': 'arg3' }