dlang-tour / english

Base language version of the Tour
https://tour.dlang.org
28 stars 73 forks source link

gem: Using Fibers with ranges #7

Open wilzbach opened 8 years ago

wilzbach commented 8 years ago

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 commented 8 years ago

From @stonemaster on May 18, 2016 14:5

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