Open In App

Lodash _.fix() Method

Last Updated : 11 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// Defining lodash contrib variable
let _ = require('lodash-contrib');
 
// Function
function mul(a, b, c, d){
    return a*b*c*d;
}
 
// Making curried function
let gfgFunc = _.fix(mul, 1, 2, 3, _);
 
// 8 is replaced by _ in given values
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




// Defining lodash contrib variable
let _ = require('lodash-contrib');
 
// Function
function add(a, b, c){
    return a + b + c;
}
 
// Making fixed function
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




// Defining lodash contrib variable
let _ = require('lodash-contrib');
 
// Function
function fun(str){
    return str;
}
 
// Making fixed function
let gfgFunc = _.fix(fun, _);
 
console.log("Coding Platform :",
    gfgFunc("GeeksforGeeks"));


Output: 

Coding Platform : GeeksforGeeks


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads