我試圖確切地了解這個(gè)功能是如何運(yùn)作的,但無(wú)法在我的大腦中弄清楚。請(qǐng)您能顯示代碼的分步過(guò)程嗎?我一直坐在這里,試圖在我的電腦上使用計(jì)算器在我的腦海中弄清楚這個(gè)例子,并在我的腦海中多次運(yùn)行代碼,但它只是沒(méi)有加起來(lái)。雙關(guān)語(yǔ)不是故意的。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??!}好的,這就是我如何在腦海中運(yùn)行它。請(qǐng)你向我解釋為什么我錯(cuò)了,并告訴我偽代碼中什么是正確的,就像我在下面所做的那樣。```// 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 等等...我在這里缺少什么?
有人可以用外行的術(shù)語(yǔ)向我解釋這個(gè)折疊示例嗎?
慕桂英3389331
2021-07-01 14:11:40