Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript';让我们用咖喱来搭配。这些代码是如何工作的?_Javascript_Functional Programming - Fatal编程技术网

JavaScript';让我们用咖喱来搭配。这些代码是如何工作的?

JavaScript';让我们用咖喱来搭配。这些代码是如何工作的?,javascript,functional-programming,Javascript,Functional Programming,这些代码两天前发布在CodeReview上: function curry(f, self) { return function () { if (arguments.length == f.length) { return f.apply(self, arguments); } arguments = Array.prototype.slice.call(arguments); return curry(f.bind.apply(f, [sel

这些代码两天前发布在CodeReview上:

function curry(f, self) {
  return function () {
    if (arguments.length == f.length) {
      return f.apply(self, arguments);
    }
    arguments = Array.prototype.slice.call(arguments);

    return curry(f.bind.apply(f, [self].concat(arguments)));
  }
}

function f(a, b, c, d) {
  return this + a + b + c + d;
}

document.write("f(1, 2, 3, 4) = ", curry(f, 0)(1, 2, 3, 4), "<br>");
document.write("f(1, 2, 3)(4) = ", curry(f, 0)(1, 2, 3)(4), "<br>");
document.write("f(1)(2, 3, 4) = ", curry(f, 0)(1)(2, 3, 4), "<br>");
document.write("f(1)(2)(3)(4) = ", curry(f, 0)(1)(2)(3)(4), "<br>");
功能咖喱(f,self){
返回函数(){
if(arguments.length==f.length){
返回f.apply(自身、参数);
}
参数=Array.prototype.slice.call(参数);
返回curry(f.bind.apply(f[self].concat(arguments));
}
}
函数f(a,b,c,d){
返回此+a+b+c+d;
}
写下(“f(1,2,3,4)=”,咖喱(f,0)(1,2,3,4),“
”; 写下(“f(1,2,3)(4)=”,咖喱(f,0)(1,2,3)(4),“
”; 文件。写(“f(1)(2,3,4)=”,咖喱(f,0)(1)(2,3,4),“
”; 文件。书写(“f(1)(2)(3)(4)=”,咖喱(f,0)(1)(2)(3)(4),“
”;
我无法理解的是:

使用bind()创建了f的新副本。已提供的参数已分配给副本,但变量“self”是什么

我试图“勾勒”我的意思:

// Second parenthesis (marked with =>): There are three of four  
// expected parameter provided: 
document.write("f(1, 2, 3)(4) = ", curry(f, 0) => (1, 2, 3) <= (4), "<br>");

// Makes an array-literal with "self" (== 0) as only element in it.
// Then adds the parameter already provided to these array by
// using concat(). => Results in an array [ 0, 1, 2, 3 ].
// Then makes a new copy of f with these values bind to it as parameter. 
// These new instance of the function is then passed to the curry-function.
return curry(f.bind.apply(f, [self].concat(arguments)));
//第二个括号(标有=>):四个括号中有三个
//提供的预期参数:
write(“f(1,2,3)(4)=”,curry(f,0)=>(1,2,3)生成一个数组[0,1,2,3]。
//然后将这些值作为参数绑定到f的新副本。
//这些新的函数实例随后被传递给curry函数。
返回curry(f.bind.apply(f[self].concat(arguments));
f的副本应该有它的四个参数。它应该被执行,结果是“返回0+0+1+2+3”;并返回6

为什么不是这样

也许有人能回答这个问题,我将不胜感激

变量“self”是什么?它用于覆盖this关键字,并已添加到给定给函数的数组中

不,不是:

  f.bind.apply(f, [self].concat(arguments))
≡ f.bind.apply(f, [self].concat([1, 2, 3]))
≡ f.bind.apply(f, [0, 1, 2, 3])
≡ f.bind(0, 1, 2, 3)
self
/
0
被绑定为
this
参数,
1
2
3
被绑定为三个部分应用的参数。此处不复制任何内容。结果是一个函数

function bound_f(x, ...args)
    return f.call(0, 1, 2, 3, x, ...args);
}
然后再次使用curry,并可以使用
4
作为参数调用

变量“self”是什么?它用于覆盖this关键字,并已添加到给定给函数的数组中

不,不是:

  f.bind.apply(f, [self].concat(arguments))
≡ f.bind.apply(f, [self].concat([1, 2, 3]))
≡ f.bind.apply(f, [0, 1, 2, 3])
≡ f.bind(0, 1, 2, 3)
self
/
0
被绑定为
this
参数,
1
2
3
被绑定为三个部分应用的参数。此处不复制任何内容。结果是一个函数

function bound_f(x, ...args)
    return f.call(0, 1, 2, 3, x, ...args);
}

然后再次使用curry,并可以使用
4
作为参数进行调用。

第二个
0
从何而来,当只有4个参数中的3个时,为什么您认为已经对其进行了计算(到
6
)?调用curry函数并将0作为第二个参数(参数“self”)。然后再计算self(===0)加上第二个调用的参数(我用=>标记了它,不管发生什么,这一定是我见过的最丑陋的curry实现之一。第二个
0
来自哪里,你认为它为什么会被计算(到
6
)当只有4个参数中的3个时,调用curry函数并将0作为第二个参数(参数“self”)。然后将self(==0)与第二个调用的参数一起计算(我已经标记了=>无论发生什么,这一定是我见过的最丑陋的咖喱实现之一。@Bergi确实解释得很好。非常感谢。@Bergi确实解释得很好。非常感谢。