Lodash _.fix() method fixes the arguments to a function based on the parameter template defined by the presence of values and the _ placeholder.
The function takes a parameter that is replaced by _ in given values.
Syntax:
_.fix( fun, [values] )
Parameters:
This method takes a single parameter as listed above and discussed below.
- fun: This parameter holds the given function.
- values: The values passed to the fun.
Return Value:
It returns a new 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.
Example 1: In this example, the code defines a function mul
that multiplies four numbers, uses the Lodash-contrib library’s _.fix
method to create a curried function with some fixed arguments, and then calculates the multiplication by providing one of the missing arguments (8) to the curried function and displays the result in the console.
Javascript
let _ = require( 'lodash-contrib' );
function mul(a, b, c, d){
return a*b*c*d;
}
let gfgFunc = _.fix(mul, 1, 2, 3, _);
console.log( "Multiplication is :" , gfgFunc(8));
|
Output:
Multiplication is : 48
Example 2: In this example, the code uses the Lodash-contrib library to define a function add
that adds three numbers, and then it creates a fixed function using the _.fix
method with some fixed arguments. The code subsequently computes the addition by providing one of the missing arguments (10) to the fixed function and displays the result in the console.
Javascript
let _ = require( 'lodash-contrib' );
function add(a, b, c){
return a + b + c;
}
let gfgFunc = _.fix(add, 1, 2, _);
console.log( "Addition is :" , gfgFunc(10));
|
Output:
Addition is : 13
Example 3: In this example, the code utilizes the Lodash-contrib library to define a function fun
that takes a string and returns it. The code then creates a fixed function using the _.fix
method, and the fixed function simply returns the provided string “GeeksforGeeks.” The result is displayed in the console as “Coding Platform: GeeksforGeeks.”
Javascript
let _ = require( 'lodash-contrib' );
function fun(str){
return str;
}
let gfgFunc = _.fix(fun, _);
console.log( "Coding Platform :" ,
gfgFunc( "GeeksforGeeks" ));
|
Output:
Coding Platform : GeeksforGeeks
Last Updated :
11 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...