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