Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest adding main() code to exercise example code for the generators on the 'Functions' page #5729

Open
1 task
dtonhofer opened this issue Apr 17, 2024 · 1 comment
Labels
a.language Relates to the Dart language tour e2-days Can complete in < 5 days of normal, not dedicated, work from.page-issue Reported in a reader-filed concern p2-medium Necessary but not urgent concern. Resolve when possible. st.triage.ltw Indicates Lead Tech Writer has triaged

Comments

@dtonhofer
Copy link

Page URL

https://dart.dev/language/functions/

Page source

https://github.com/dart-lang/site-www/tree/main/src/content/language/functions.md

Describe the problem

The explainer for Generators at

https://dart.dev/language/functions#generators

shows ways to create generator Iterable-s or generator Stream-s, but does not give example code for using them.

This may be too verbose for the page, but I suggest just adding a main() to exercise the example code, which is particularly interesting for the case of using a Stream:

For the "synchronous generator function":

Iterable<int> naturalsTo(int n) sync* {
  int k = 0;
  while (k < n) {
    print("Yielding $k in Iterable");
    yield k++;
  }
}

main() async {
  for (var n in naturalsTo(2)) {
    print("Got $n from Iterable");
  }
  print("Bye");
}

which outputs

Yielding 0 in Iterable
Got 0 from Iterable
Yielding 1 in Iterable
Got 1 from Iterable
Bye

For the "asynchronous generator function":

Stream<int> asynchronousNaturalsTo(int n) async* {
  int k = 0;
  while (k < n) {
    print("Yielding $k in Stream");
    yield k++;
  }
}

main() async {
  var streamOfString =
      asynchronousNaturalsTo(2).map((n) => "Got $n from Stream");
  // The stream is "run" by collecting its elements into a list
  // and await-ing.
  for (var str in await streamOfString.toList()) {
    print(str);
  }
  print("Bye");
}

which outputs

Yielding 0 in Stream
Yielding 1 in Stream
Got 0 from Stream
Got 1 from Stream
Bye

For the yield* generator function (what does it do? tail call optimization?)

Iterable<int> naturalsDownFrom(int n) sync* {
  if (n > 0) {
    print("Yielding $n in Stream based on recursive generator");
    yield n;
    yield* naturalsDownFrom(n - 1);
  }
}

main() async {
  for (var n in naturalsDownFrom(2)) {
    print("Got $n from Iterable based on recursive generator");
  }
  print("Bye");
}

which outputs

Yielding 2 in Stream based on recursive generator
Got 2 from Iterable based on recursive generator
Yielding 1 in Stream based on recursive generator
Got 1 from Iterable based on recursive generator
Bye

Expected fix

No response

Additional context

No response

I would like to fix this problem.

  • I will try and fix this problem on dart.dev.
@dtonhofer dtonhofer added the from.page-issue Reported in a reader-filed concern label Apr 17, 2024
@atsansone atsansone changed the title [PAGE ISSUE]: 'Functions' - suggesting adding main() code to exercise example code for the generators Suggest adding main() code to exercise example code for the generators on the 'Functions' page Apr 18, 2024
@atsansone atsansone added a.language Relates to the Dart language tour p2-medium Necessary but not urgent concern. Resolve when possible. e2-days Can complete in < 5 days of normal, not dedicated, work st.triage.ltw Indicates Lead Tech Writer has triaged labels Apr 18, 2024
@munificent
Copy link
Member

Yeah, showing an example of calling the generator seems like it would help here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a.language Relates to the Dart language tour e2-days Can complete in < 5 days of normal, not dedicated, work from.page-issue Reported in a reader-filed concern p2-medium Necessary but not urgent concern. Resolve when possible. st.triage.ltw Indicates Lead Tech Writer has triaged
Projects
None yet
Development

No branches or pull requests

3 participants