简体   繁体   English

如何在 javascript 中实现柯里化?

[英]How to implement currying in javascript?

Question as below:问题如下:

create a sum function, and the requirement:

sum(1,2).result === 3
sum(1,2)(3).result == 6
sum(1,2)(3,4).result == 10
sum(1,2)(3,4)(5).result == 15

This is a question about currying in JS.这是一个关于 JS 中柯里化的问题。 I have implemented the most functions of the question.我已经实现了这个问题的大部分功能。 The tricky point is .result for me.棘手的一点是 .result 对我来说。

What does .result means after sum(1,2)? sum(1,2) 之后的.result 是什么意思? Is it an attribute?是属性吗?

How to add the .result to my code?如何将 .result 添加到我的代码中?

 function sum(){ var count = 0; for(let i=0; i<arguments.length; i++){ count += arguments[i]; } var tmp = function(){ for(let i=0; i<arguments.length; i++){ count += arguments[i]; } return tmp; } tmp.toString = function(){ return count; } return tmp; } console.log(sum(1,2)) console.log(sum(1,2)(3))

You can add .result to your code by storing result as a property in function.您可以通过将结果存储为函数中的属性来将 .result 添加到您的代码中。

You can also remove the redundant tmp function code .您还可以删除多余的tmp功能代码。 Here is how it looks:这是它的外观:

function sum(){
    var count = 0;    // use previous result
    for(let i=0; i<arguments.length; i++){
        count += arguments[i];
    }
    const newSum = sum.bind(this,count);
    newSum.result = count;
    return newSum;
}

console.log(sum(1,2).result)
console.log(sum(1,2)(3).result)

.bind() here binds the first argument of cloned function (newSum) as count , which will be used on all subsequent calls .bind()这里将克隆函数 (newSum) 的第一个参数绑定为 count ,它将用于所有后续调用

Shrey answer is Perfect but we can make it more sorter and clear. Shrey 的回答是完美的,但我们可以让它更加分类和清晰。

 function sum() { let result = [...arguments].reduce((acc, num) => acc + num, 0); let newSum = sum.bind(this, result); newSum.result = result; return newSum; } console.log(sum(3,4)(5)(9).result); console.log(sum(3,4)(5,6)(9).result);

Currying is an advanced technique of working with functions.柯里化是一种使用函数的高级技术。 It's used not only in JavaScript, but in other languages as well.它不仅用于 JavaScript,还用于其他语言。

Currying is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c).柯里化是一种函数转换,它将函数从可调用的f(a, b, c)转换为可调用的f(a)(b)(c).

Currying doesn't call a function.柯里化不会调用函数。 It just transforms it.它只是改变它。

Let's see an example first, to better understand what we're talking about, and then practical applications.让我们先看一个例子,以便更好地理解我们在说什么,然后是实际应用。

We'll create a helper function curry(f) that performs currying for a two-argument f .我们将创建一个辅助函数curry(f)来为两个参数f执行柯里化。 In other words, curry(f) for two-argument f(a, b) translates it into a function that runs as f(a)(b) :换句话说,对于两个参数f(a, b) curry(f)将其转换为以f(a)(b)运行的函数:

function curry(f) { // curry(f) does the currying transform
  return function(a) {
    return function(b) {
      return f(a, b);
    };
  };
}

// usage
function sum(a, b) {
  return a + b;
}

let curriedSum = curry(sum);

alert( curriedSum(1)(2) ); // 3

Also Refer this link for more details另请参阅此链接以获取更多详细信息

Here a solution.这里有一个解决方案。 It always better to use spread operator on the arguments instead of using arguments keyword.在参数上使用扩展运算符总是更好,而不是使用arguments关键字。 Witch should be avoided应避免女巫

 function sum(...args){ let count = 0; count += args.reduce((a,v) => a + v); let res = sum.bind(this,count); res.result = count; return res; } console.log(sum(1,5)(4)(5).result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM