english icon indicating copy to clipboard operation
english copied to clipboard

gem: Using Fibers with ranges

Open wilzbach opened this issue 9 years ago • 1 comments

From @wilzbach on May 18, 2016 13:12

This is one of my favorite features in D, but it took me a while to discover this. This allows one to write pretty sophisticated lazy algorithms by just saving keeping a reference to the stack. Ali had a great talk about this year -> http://dconf.org/2016/talks/cehreli.html

import std.concurrency: Generator,yield;
auto r = new Generator!int(
{
    foreach (i; 1 .. 10)
        yield(i);
});

import std.range: iota;
import std.algorithm.comparison: equal;
assert(r.equal(iota(1, 10)));

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

Copied from original issue: stonemaster/dlang-tour#133

wilzbach avatar Aug 21 '16 14:08 wilzbach

From @stonemaster on May 18, 2016 14:5

That's a good one. There is already fiber's section maybe it fits there.

wilzbach avatar Aug 21 '16 14:08 wilzbach