Skip to content

Instantly share code, notes, and snippets.

@BoshiLee
Created November 21, 2019 08:03
Show Gist options
  • Save BoshiLee/8d982233dc95036108600e3c70048971 to your computer and use it in GitHub Desktop.
Save BoshiLee/8d982233dc95036108600e3c70048971 to your computer and use it in GitHub Desktop.
Stream<int> countStream(int max) async* {
for (int i = 0; i < max; i++) {
yield i;
}
}
Future<int> sumStream(Stream<int> stream) async {
int sum = 0;
await for (int value in stream) {
sum += value;
}
return sum;
}
void main() async {
/// Initialize a stream of integers 0-9
Stream<int> stream = countStream(10);
/// Compute the sum of the stream of integers
int sum = await sumStream(stream);
/// Print the sum
print(sum); // 45
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment