📜  Lodash _.curry2() 方法

📅  最后修改于: 2022-05-13 01:56:31.921000             🧑  作者: Mango

Lodash _.curry2() 方法

Lodash _.curry2()方法返回给定函数的柯里化版本,但将恰好柯里化两个参数,不多也不少。

句法:

_.curry2( fun )

参数:此方法采用上面列出并在下面讨论的单个参数。

  • fun:这是给定的函数。

返回值:它返回函数的柯里化版本。

注意:要执行以下示例,您必须使用此命令提示符安装lodash-contrib库并执行以下命令。

npm install lodash-contrib

示例 1:

Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function fun(a, b){
    return a * b;
}
  
// Making curried function
var gfgFunc = _.curry2(fun);
  
// Only operates for exactly two arguments
console.log("Multiplication is :",
    gfgFunc(2)(23));


Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function fun(a, b){
    return a + b;
}
  
// Making curried function
var gfgFunc = _.curry2(fun);
  
// Only operates for exactly two arguments
console.log("Addition is :", gfgFunc(25)(23));


Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function fun(x, y){
    return arguments;
}
  
// Making curried function
var gfgFunc = _.curry2(fun);
  
// Shows only two arguments
console.log("Curried Arguments are :",
    gfgFunc("arg1")("arg2"));


输出:

Multiplication is : 46

示例 2:

Javascript

// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function fun(a, b){
    return a + b;
}
  
// Making curried function
var gfgFunc = _.curry2(fun);
  
// Only operates for exactly two arguments
console.log("Addition is :", gfgFunc(25)(23));

输出:

Addition is : 48

示例 3:

Javascript

// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function fun(x, y){
    return arguments;
}
  
// Making curried function
var gfgFunc = _.curry2(fun);
  
// Shows only two arguments
console.log("Curried Arguments are :",
    gfgFunc("arg1")("arg2"));

输出:

Curried Arguments are : [Arguments] 
    { '0': 'arg1', '1': 'arg2' }