📜  Lodash _.curryRight2() 方法

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

Lodash _.curryRight2() 方法

Lodash 是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。

_.curryRight2() 方法用于返回给定函数的柯里化版本,其中最多从右到左处理两个参数。

句法:

_.curryRight2( 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) {
    return a / b;
}
  
// Using the _.curryRight2() method
var curried = _.curryRight2(div);
  
console.log("Curried division is :", 
    curried(4)(32));


Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function to curry
function div(a, b) {
    return a / b;
}
  
// Using the _.curryRight2() method
var curried = _.curryRight2(div);
  
console.log("Curried division is :", 
    curried(32)(4));


Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function to curry
function sub(a, b) {
    return a - b;
}
  
// Using the _.curryRight2() method
var curried = _.curryRight2(sub);
  
console.log("Curried Subtraction is :",
    curried(2)(10));


Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function to curry
function div(a, b) {
    return ("a=" + a + " and b=" + b);
}
  
// Using the _.curryRight2() method
var curried = _.curryRight2(div);
  
console.log(curried("a")("b"));


输出:

Curried division is : 8

示例 2:

Javascript

// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function to curry
function div(a, b) {
    return a / b;
}
  
// Using the _.curryRight2() method
var curried = _.curryRight2(div);
  
console.log("Curried division is :", 
    curried(32)(4));

输出:

Curried division is : 0.125

示例 3:

Javascript

// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function to curry
function sub(a, b) {
    return a - b;
}
  
// Using the _.curryRight2() method
var curried = _.curryRight2(sub);
  
console.log("Curried Subtraction is :",
    curried(2)(10));

输出:

Curried Subtraction is : 8

示例 4:

Javascript

// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function to curry
function div(a, b) {
    return ("a=" + a + " and b=" + b);
}
  
// Using the _.curryRight2() method
var curried = _.curryRight2(div);
  
console.log(curried("a")("b"));

输出:

a=b and b=a