Эта страница ещё не переведена, поэтому показана на английском. Помогите с переводом

fxMapAccum

A running accumulation, one value out for each value in — scan without the seed.

Iterable<B> fxMapAccum<A, B>(B Function(B acc, A a) f, B seed, Iterable<A> iterable) FxAsyncIterable<B> fxMapAccumAsync<A, B>(FutureOr<B> Function(B acc, A a) f, FutureOr<B> seed, FxAsyncIterable<A> iterable) Fx<B> Fx<T>.mapAccum<B>(B Function(B acc, T a) f, B seed) // chain (sync) FxAsync<B> FxAsync<T>.mapAccum<B>(FutureOr<B> Function(B acc, T a) f, FutureOr<B> seed) // chain (async)

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).

Related: scan — the same fold, seed included · reduce/fold — only the final value · drop — what mapAccum saves you from · concurrent — parallel evaluation upstream