Skip to content

Instantly share code, notes, and snippets.

@Sfshaza
Last active September 9, 2015 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sfshaza/15d5ef986238c97dbc14 to your computer and use it in GitHub Desktop.
Save Sfshaza/15d5ef986238c97dbc14 to your computer and use it in GitHub Desktop.
streams/simple_stream
// Copyright (c) 2015, the Dart project authors.
// Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed
// by a BSD-style license that can be found in the LICENSE file.
import 'dart:async';
Future<int> sumStream(Stream<int> stream) async {
var sum = 0;
await for (var value in stream) {
sum += value;
}
return sum;
}
Stream<int> countStream(int to) async* {
for (int i = 1; i <= to; i++) {
yield i;
}
}
main() async {
var stream = countStream(10);
var sum = await sumStream(stream);
print(sum); // 55
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment