reduce method

Future<T> reduce(T combine(T previous, T element))

Reduces a sequence of values by repeatedly applying combine.

Source

Future<T> reduce(T combine(T previous, T element)) {
  _Future<T> result = new _Future<T>();
  bool seenFirst = false;
  T value;
  StreamSubscription subscription;
  subscription = this.listen(
      (T element) {
        if (seenFirst) {
          _runUserCode(() => combine(value, element), (T newValue) {
            value = newValue;
          }, _cancelAndErrorClosure(subscription, result));
        } else {
          value = element;
          seenFirst = true;
        }
      },
      onError: result._completeError,
      onDone: () {
        if (!seenFirst) {
          try {
            throw IterableElementError.noElement();
          } catch (e, s) {
            _completeWithErrorCallback(result, e, s);
          }
        } else {
          result._complete(value);
        }
      },
      cancelOnError: true);
  return result;
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/1.24.3/dart-async/Stream/reduce.html