scan<S> method

Stream<S> scan<S>(
  1. S initialValue,
  2. FutureOr<S> combine(
    1. S soFar,
    2. T element
    )
)

Emits a sequence of the accumulated values from repeatedly applying combine.

Like fold, but instead of producing a single value it yields each intermediate result.

If combine returns a future it will not be called again for subsequent events from the source until it completes, therefore combine is always called for elements in order, and the result stream always maintains the same order as this stream.

Implementation

Stream<S> scan<S>(
    S initialValue, FutureOr<S> Function(S soFar, T element) combine) {
  var accumulated = initialValue;
  return asyncMap((value) {
    var result = combine(accumulated, value);
    if (result is Future<S>) {
      return result.then((r) => accumulated = r);
    } else {
      return accumulated = result;
    }
  });
}