Created
November 21, 2019 08:03
-
-
Save BoshiLee/8d982233dc95036108600e3c70048971 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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