Skip to content

Commit c278d38

Browse files
committed
Add Generators
1 parent 17bdfaa commit c278d38

18 files changed

+37
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

generators.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// Dart has support for both asynchronous and synchronous generators.
2+
import "dart:async";
3+
4+
Stream<int> read() async* {
5+
var i = 0;
6+
while (i < 5) {
7+
i++;
8+
yield i; // Pauses execution here, waits for receiver to iterate, asynchronously.
9+
}
10+
}
11+
12+
Iterable<int> readSync() sync* {
13+
var i = 0;
14+
while (i < 5) {
15+
i++;
16+
yield i; // Pauses execution here, waits for receiver to iterate.
17+
}
18+
}
19+
20+
Iterable<int> readMoreSync() sync* {
21+
yield* readSync(); // You can also use yield* to yield another iterator or stream.
22+
yield 10;
23+
}
24+
25+
main() async {
26+
await for (var i in read()) {
27+
print("Asynchronous Generator: ${i}");
28+
}
29+
30+
for (var i in readSync()) {
31+
print("Synchronous Generator: ${i}");
32+
}
33+
34+
for (var i in readMoreSync()) {
35+
print("More Synchronous Generator: ${i}");
36+
}
37+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

bin/try.dart renamed to try.dart

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)