Lodash _.curry() Method
Last Updated :
31 Oct, 2023
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
let _ = require( 'lodash' );
function fun(a, b, c, d) {
return a + b + c + d;
}
let gfgFunc = _.curry(fun);
console.log( "Addition is :" ,
gfgFunc(42)(23)(20)(30));
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
let _ = require( 'lodash' );
function fun(a, b, c) {
return a * b * c;
}
let gfgFunc = _.curry(fun);
console.log( "Multiplication is :" ,
gfgFunc(20)(23)(27));
console.log(gfgFunc(1)(2));
|
Output:
Multiplication is : 12420
[Function: wrapper]
Share your thoughts in the comments
Please Login to comment...