Generator

A Generator is a Fiber that periodically returns values of type T to the caller via yield. This is represented as an InputRange.

Constructors

this
this(void function() fn)

Initializes a generator object which is associated with a static D function. The function will be called once to prepare the range for iteration.

this
this(void function() fn, size_t sz)

Initializes a generator object which is associated with a static D function. The function will be called once to prepare the range for iteration.

this
this(void function() fn, size_t sz, size_t guardPageSize)

Initializes a generator object which is associated with a static D function. The function will be called once to prepare the range for iteration.

this
this(void delegate() dg)

Initializes a generator object which is associated with a dynamic D function. The function will be called once to prepare the range for iteration.

this
this(void delegate() dg, size_t sz)

Initializes a generator object which is associated with a dynamic D function. The function will be called once to prepare the range for iteration.

this
this(void delegate() dg, size_t sz, size_t guardPageSize)

Initializes a generator object which is associated with a dynamic D function. The function will be called once to prepare the range for iteration.

Members

Functions

empty
bool empty()

Returns true if the generator is empty.

front
T front()

Returns the most recently generated value by shallow copy.

moveFront
T moveFront()

Returns the most recently generated value without executing a copy contructor. Will not compile for element types defining a postblit, because Generator does not return by reference.

popFront
void popFront()

Obtains the next value from the underlying function.

Examples

1 auto tid = spawn({
2     int i;
3     while (i < 9)
4         i = receiveOnly!int;
5 
6     ownerTid.send(i * 2);
7 });
8 
9 auto r = new Generator!int({
10     foreach (i; 1 .. 10)
11         yield(i);
12 });
13 
14 foreach (e; r)
15     tid.send(e);
16 
17 assert(receiveOnly!int == 18);

Meta

Suggestion Box / Bug Report