fxMapAccum
A running accumulation, one value out for each value in — scan without the seed.
Lecture
scan emits its seed first, so
n values in give n + 1 out. That is right when
the starting state is part of the answer — a ledger's opening balance —
and wrong when it is only the state the first element folds into, which
is why so many scan calls end in a trailing
.drop(1). fxMapAccum(f, seed, xs) is that
shape as one operator: it folds seed into each value with
f and emits every intermediate accumulation, but never the
seed itself. n in, n out — an empty input
emits nothing at all.
The argument order matches fxScan on purpose, so moving
from one to the other is a one-word edit. It is lazy, f
runs exactly once per pulled element, in order, and the accumulator
belongs to the iteration: iterating twice starts from
seed both times. The accumulator type B need
not match the element type — fold strings into a path, numbers into a
record.
fxMapAccumAsync lets both f and
seed be a Future. The fold is serialized —
each step needs the previous one — but an upstream stage can still run
under concurrent(n), as in
Demo 2. On the chain it is fx(xs).mapAccum(f, seed).
fxdart extension (not part of FxTS) — Rust's
Iterator::scan, Haskell's mapAccumL and
RxDart's scan emit the same n values.
Demo 1 · scan, minus the seed
Demo 2 · Async, with concurrency upstream
Try it yourself
Exercise: drop the trailing drop(1).
scan — the same fold, seed included ·
reduce/fold — only the final value ·
drop — what mapAccum saves you from ·
concurrent — parallel evaluation upstream