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

Generators (yield return) #101

Open
WebFreak001 opened this issue Feb 20, 2016 · 1 comment
Open

Generators (yield return) #101

WebFreak001 opened this issue Feb 20, 2016 · 1 comment

Comments

@WebFreak001
Copy link

https://dlang.org/library/std/concurrency/generator.html

They basically allow the programmer to make every function into a phobos range.

Instead of iterating through stuff and calculating things you could simply use a generator which will only calculate the first thing and then return that one. Once another one is requested it will start from where it stopped.

import std.concurrency;
import std.stdio;
import std.range;

auto generateIntegers()
{
  return new Generator({
    int i = 0;
    while(true) // wont get stuck here
      yield(i++); // because of yield
  });
}

void main()
{
  generateIntegers().take(50).each!writeln;
  // Will print 0-49 to the console
}

More information in other languages: http://www.dotnetperls.com/yield

@p0nce
Copy link
Owner

p0nce commented Feb 20, 2016

Could be in "Phobos gems" article, but probably better in its own article.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants