generate with state

Freddy via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 2 16:51:15 PST 2015


On Tuesday, 3 November 2015 at 00:08:54 UTC, Ali Çehreli wrote:
> generate() already allows "callables", which can be a delegate:
>
> import std.stdio;
> import std.range;
>
> struct S {
>     int i;
>
>     int fun() {
>         return i++;
>     }
> }
>
> void main() {
>     auto s = S(42);
>     writefln("%(%s %)", generate(&s.fun).take(5));
> }
>
> Prints
>
> 42 43 44 45 46

Will that allocate gc memory? Is there any why I pass state as a 
tuple and have my generator modify state as It's called?



More information about the Digitalmars-d-learn mailing list