Underscore.js _.curry2() 方法
Underscore.js _.curry2()方法返回给定函数的柯里化版本,但会柯里化两个参数,不多也不少。
句法:
_.curry2( fun )
参数:此方法采用上面列出并在下面讨论的单个参数:
- fun:这是作为参数传递的给定函数。
返回值:它返回函数的柯里化版本。
注意:要执行以下示例,您必须使用此命令提示符安装underscore-contrib库并执行以下命令。
npm install underscore-contrib
示例 1:
// Defining underscore contrib variable
var _ = require('underscore-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(20)(23));
输出:
Multiplication is : 460
示例 2:
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Function
function fun(a, b) {
return a-b;
}
// Making curried function
var gfgFunc = _.curry2(fun);
// Only operates for exactly two arguments
console.log("Subtraction is :",
gfgFunc(25)(23));
输出:
Subtraction is : 2
示例 3:打印 curry2()函数采用的参数。
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Function
function fun(x, y) {
return arguments;
}
// Making curried function
var gfgFunc = _.curry2(fun);
// Only operates for exactly two arguments
console.log("Curried Arguments are :",
gfgFunc("a")("b"));
输出:
Curried Arguments are : [Arguments] { '0': 'a', '1': 'b' }