<!-- 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>
<!– first –> <script> var total = 0; var newAccumulator = function() { return
Share
In the call to
foldlyou call thenewAccumulatorfunction:In the first case this returns a function that does the summing up:
In the second case the call to
newAccumulatordoesn’t return anything, sofoldldoesn’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: