Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import "dart:async"; | |
| 6 | |
| 7 /// Updates an accumulator with each event and emits the intermediate results. | |
| 8 /// | |
| 9 /// Combines each event with the previous accumulator value | |
| 10 /// and emits the new result. | |
| 11 /// | |
| 12 /// Example: | |
| 13 /// ```dart | |
| 14 /// var stream = new Stream.fromIterable([1, 2, 3, 4, 5]); | |
| 15 /// var result = await stream.transform(new Scan(1, (a, b) => a * b)).toList(); | |
| 16 /// print(result); // [1, 2, 6, 24, 120] | |
| 17 /// ``` | |
| 18 /// | |
| 19 /// The last result emitted by `stream.transform(new Scan(a, c))` | |
| 20 /// is the same result that would be computed by `stream.fold(a, c)`. | |
| 21 /// | |
| 22 /// Errors in the source stream or from calling the combine function | |
| 23 /// are reported on the result stream and abort the transformation. | |
| 24 class Scan<S, A> implements StreamTransformer<S, A> { | |
|
floitsch
2016/02/26 13:50:44
I'm still not convinced that "Scan" is a good name
nweiz
2016/03/01 02:10:04
I think matching Reactive here is worth the odd na
| |
| 25 final A _initial; | |
| 26 final Function _combine; | |
| 27 | |
| 28 /// Accumulates stream events using [combine] starting with [initial]. | |
| 29 Scan(A initial, A combine(A accumulator, S source)) | |
| 30 : _initial = initial, _combine = combine; | |
| 31 | |
| 32 Stream<A> bind(Stream<S> stream) async* { | |
| 33 A accumulator = _initial; | |
| 34 await for (S source in stream) { | |
| 35 accumulator = _combine(accumulator, source); | |
| 36 yield accumulator; | |
| 37 } | |
| 38 } | |
| 39 } | |
| OLD | NEW |