Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: lib/src/transformers/scan.dart

Issue 1648963002: Add reactive-inspired stream transformers: Base URL: https://github.com/dart-lang/async@master
Patch Set: Restructure failes and add more tests. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698