Lodash _.curry() Method
Lodash _.curry() method returns a curried version of the given function. it accepts an arity which is a number that shows it will keep accepting arguments until it has received this number of arguments or invocings.
Syntax:
_.curry(fun, [arity=fun.length])
Parameters:
- fun: This is the given function.
[arity=fun.length]
: The arity of the function
Return Value:
It returns a curried version of the function.
Example 1: In this example, we are passing numbers to the gfgFunc which stores the result of the lodash _.curry method and prints the result in the console.
Javascript
// Defining lodash contrib variable let _ = require( 'lodash' ); // Function function fun(a, b, c, d) { return a + b + c + d; } // Making curried function let 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: In this example, we are passing numbers to the gfgFunc which stores the result of the lodash _.curry method and prints the result in the console.
Javascript
// Defining lodash contrib variable let _ = require( 'lodash' ); // Function function fun(a, b, c) { return a * b * c; } // Making curried function let 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]