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

auto tid = spawn({
    int i;
    while (i < 9)
        i = receiveOnly!int;

    ownerTid.send(i * 2);
});

auto r = new Generator!int({
    foreach (i; 1 .. 10)
        yield(i);
});

foreach (e; r)
    tid.send(e);

assert(receiveOnly!int == 18);

Meta