Underscore.js _.rCurry() 方法
Underscore.js _.rCurry() 方法返回给定函数的柯里化版本,其中参数从右到左处理。
句法:
_.rCurry( fun )
参数:此方法采用上面列出并在下面讨论的单个参数:
- fun:这是作为参数传递的给定函数。
返回值:此方法返回一个柯里函数。
注意:要执行以下示例,您必须使用此命令提示符安装underscore-contrib库并执行以下命令。
npm install underscore-contrib
示例 1:
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Function
function div(a, b, c) {
return a/b/c;
}
var curried = _.rCurry(div);
console.log("Curried division is :",
curried(2)(4)(16));
输出:
Curried division is : 2
示例 2:
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Function
function div(a, b, c, d) {
return a-b-c-d;
}
var curried = _.rCurry(div);
console.log("Curried Subtraction is :",
curried(2)(4)(16)(44));
输出:
Curried Subtraction is : 22
示例 3:参数颠倒如下所示:
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Function
function div(a, b, c) {
return ("a="+a+" and b="+b+" and c="+c);
}
var curried = _.rCurry(div);
console.log(curried("a")("b")("c"));
输出:
a=c and b=b and c=a