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;
}