Forum Discussion
How - and when - does the accumulator of SCAN get reset?
- Jan 05, 2025
I think your question is really about how the accumulator in SCAN works and the answer is that it is NOT a good term at all. The "accumulator" is really just the previous resulting value and althought the term "accumulator" is technically correct, it can be confusing. I prefer to use "p" and "q" as in SCAN( _init, _array, LAMBDA( p, q, ...)) because "p" is the prior or previous value (and on the first iteration it will use the _init value you give it) and the "q" is the qth value in the _array. so it will iterate through the _array and at the end give a final answer (and because it is SCAN instead of REDUCE it will also give you every iteration on the way) based on the formula you give. The term "accumulator" is technically correct because when it goes into the 7th iteration it will pass the result from the 6th iteration but that result from the 6th iteration is technically the accumulated result from ALL the previous iterations. Hope that help.
In re: the first example with the names. The initial value supplied in SCAN is arbitrary because the first position is the array is filled.
I believe the initial value becomes useful for filling in gaps in the data when the data is a bit messier such as:
If we're to presume those first few blanks are to be filled with "John" then the initial value becomes useful because it gets the ball rolling:
=LET(
initial, TAKE(TRIMRANGE(names2, 1), 1),
SCAN(initial, names2, FillDownλ)
)
With FillDownλ being the standard:
FillDownλ = LAMBDA(acc,v,IF(v="",acc,v))
Patrick2788 A minor variation might be
= LET(
initial, TAKE(TRIMRANGE(names2, 3), -1),
SCAN(initial, names2, FillDownλ)
)
BTW I like your use of TRIMRANGE. I opposed the introduction of the function at first.