我试图确切地了解这个功能是如何运作的,但无法在我的大脑中弄清楚。请您能显示代码的分步过程吗?
我一直坐在这里,试图在我的电脑上使用计算器在我的脑海中弄清楚这个例子,并在我的脑海中多次运行代码,但它只是没有加起来。双关语不是故意的。
constructor(values) {
this.values = values || []
}
const list = new List([1,2,3,4])
expect(list.foldl((acc, el) => el / acc, 24)).toEqual(64);
foldl(fn, initialValue) {
let acc = initialValue;
for (let i in this.values) {
acc = fn(acc, this.values[i]);
}
return acc; // 64... HOW??!
}
好的,这就是我如何在脑海中运行它。请你向我解释为什么我错了,并告诉我伪代码中什么是正确的,就像我在下面所做的那样。
```
// accumulator is 24 and therefore we divide the first element of the array which is 1 by 24 which equals .041666667
// the accumulator now ACCUMULATES which means 24 plus .041666667 is equal to 24.041666667
// now the accumulator is 24.041666667 and we divide the second element of the array which is 2 by 24 which equals .083333333
// the accumulator which is 24.041666667 now adds .083333333 which equals 24.874999997
// now the accumulator is 24.874999997 and we divide the third element of the array which is 3 by 24.874999997 which equals .120603015
等等...
我在这里缺少什么?
陪伴而非守候
相关分类