<!-- first -->
<script>
var total = 0;
var newAccumulator = function()
{
return function(i) { total += i; };
}
var foldl = function(arr, putNum)
{
for (var i = 0; i < arr.length; ++i)
{
putNum(arr[i]);
}
}
foldl([1, 2, 3, 4], newAccumulator());
document.write("Sum: " + total + "<br/>");
</script>
<!-- second -->
<script>
var total = 0;
var newAccumulator = function(i)
{
total += i;
}
var foldl = function(arr, putNum)
{
for (var i = 0; i < arr.length; ++i)
{
putNum(arr[i]);
}
}
foldl([1, 2, 3, 4], newAccumulator());
document.write("Sum: " + total + "<br/>");
</script>
4 个解决方案
#1
In the call to foldl
you call the newAccumulator
function:
在对foldl的调用中,您调用newAccumulator函数:
foldl([1, 2, 3, 4], newAccumulator());
In the first case this returns a function that does the summing up:
在第一种情况下,这将返回一个执行求和的函数:
return function(i) { total += i; };
In the second case the call to newAccumulator
doesn't return anything, so foldl
doesn't have a function it can call to calculate the sum.
You should pass newAccummulator directly to foldl
, not the value it (doesn't) return:
在第二种情况下,对newAccumulator的调用不返回任何内容,因此foldl没有可以调用来计算总和的函数。你应该将newAccummulator直接传递给foldl,而不是它(不)返回的值:
foldl([1, 2, 3, 4], newAccumulator);
#2
It think you want
它认为你想要的
foldl([1, 2, 3, 4], newAccumulator);
In the second call
在第二个电话中
#3
You are executing newAccumulator in the fold1 function. Pass in newAccumulator instead of newAccumulator();
您正在fold1函数中执行newAccumulator。传入newAccumulator而不是newAccumulator();
old
foldl([1, 2, 3, 4], newAccumulator());
new
foldl([1, 2, 3, 4], newAccumulator);
#4
The second doesn't work because you aren't passing foldl a function.
第二个不起作用,因为你没有传递foldl函数。
In the first example, you execute newAccumulator, and newAccumulator returns a function, which is passed to foldl... Foldl uses that function to sum the numbers.
在第一个示例中,执行newAccumulator,newAccumulator返回一个函数,该函数传递给foldl ... Foldl使用该函数对数字求和。
In the second example, you execute newAccumulator and pass the result, but the result of newAccumulator is not a function.
在第二个示例中,执行newAccumulator并传递结果,但newAccumulator的结果不是函数。
Also, the function you've named foldl is usually called 'foreach'. If you stored the results in an array it might be called 'map'. Foldl would normally accumulate the total itself by taking a function that adds the number to the total and returns the new total.
此外,您命名为foldl的函数通常称为“foreach”。如果将结果存储在数组中,则可能将其称为“map”。 Foldl通常会通过将一个函数添加到总数并返回新总数来累积总数。
#1
In the call to foldl
you call the newAccumulator
function:
在对foldl的调用中,您调用newAccumulator函数:
foldl([1, 2, 3, 4], newAccumulator());
In the first case this returns a function that does the summing up:
在第一种情况下,这将返回一个执行求和的函数:
return function(i) { total += i; };
In the second case the call to newAccumulator
doesn't return anything, so foldl
doesn't have a function it can call to calculate the sum.
You should pass newAccummulator directly to foldl
, not the value it (doesn't) return:
在第二种情况下,对newAccumulator的调用不返回任何内容,因此foldl没有可以调用来计算总和的函数。你应该将newAccummulator直接传递给foldl,而不是它(不)返回的值:
foldl([1, 2, 3, 4], newAccumulator);
#2
It think you want
它认为你想要的
foldl([1, 2, 3, 4], newAccumulator);
In the second call
在第二个电话中
#3
You are executing newAccumulator in the fold1 function. Pass in newAccumulator instead of newAccumulator();
您正在fold1函数中执行newAccumulator。传入newAccumulator而不是newAccumulator();
old
foldl([1, 2, 3, 4], newAccumulator());
new
foldl([1, 2, 3, 4], newAccumulator);
#4
The second doesn't work because you aren't passing foldl a function.
第二个不起作用,因为你没有传递foldl函数。
In the first example, you execute newAccumulator, and newAccumulator returns a function, which is passed to foldl... Foldl uses that function to sum the numbers.
在第一个示例中,执行newAccumulator,newAccumulator返回一个函数,该函数传递给foldl ... Foldl使用该函数对数字求和。
In the second example, you execute newAccumulator and pass the result, but the result of newAccumulator is not a function.
在第二个示例中,执行newAccumulator并传递结果,但newAccumulator的结果不是函数。
Also, the function you've named foldl is usually called 'foreach'. If you stored the results in an array it might be called 'map'. Foldl would normally accumulate the total itself by taking a function that adds the number to the total and returns the new total.
此外,您命名为foldl的函数通常称为“foreach”。如果将结果存储在数组中,则可能将其称为“map”。 Foldl通常会通过将一个函数添加到总数并返回新总数来累积总数。