Lodash _.curryRight() 方法
Lodash 是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。
_.curryRight() 方法用于返回给定函数的柯里化版本,其中给定参数从右到左处理。
句法:
_.curryRight( fun )
参数:此方法采用上面列出并在下面讨论的单个参数。
- fun:这是柯里化版本中应该使用的函数。
返回值:该方法返回柯里函数。
注意:此方法在普通 JavaScript 中不起作用,因为它需要安装 Lodash contrib 库。可以使用npm install lodash-contrib –save 安装 lodash- contrib 库
示例 1:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Function to curry
function div(a, b, c) {
return a / b / c;
}
// Using the _.curryRight() method
var curried = _.curryRight(div);
console.log("Curried division is :",
curried(3)(3)(99));
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Function to curry
function div(a, b, c, d) {
return a - b - c - d;
}
// Using the _.curryRight() method
var curried = _.curryRight(div);
console.log("Curried Subtraction is :",
curried(2)(1)(6)(44));
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Function to curry
function div(a, b, c) {
return ("a=" + a + " and b=" + b +
" and c=" + c);
}
// Using the _.curryRight() method
var curried = _.curryRight(div);
console.log(curried("a")("b")("c"));
输出:
Curried division is : 11
示例 2:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Function to curry
function div(a, b, c, d) {
return a - b - c - d;
}
// Using the _.curryRight() method
var curried = _.curryRight(div);
console.log("Curried Subtraction is :",
curried(2)(1)(6)(44));
输出:
Curried Subtraction is : 35
示例 3:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Function to curry
function div(a, b, c) {
return ("a=" + a + " and b=" + b +
" and c=" + c);
}
// Using the _.curryRight() method
var curried = _.curryRight(div);
console.log(curried("a")("b")("c"));
输出:
a=c and b=b and c=a