The Freya Programming Language

Iterators

See also

Iterators are executable members that help with the implementation of the iteration pattern.

Iterator declarations

An iterator is declared like any method, but the iterator keyword replaces the method mark:

Stack = class[X]
    // ...
    iterator Items: X;
end;

The above iterator declaration is approximately equivalent to this method declaration:

    method Items: IEnumerable[X];

Implementing an iterator

An iterator must compute a sequence of values that will be consumed by another piece of code. To notify the consumer that another value is ready, you execute a yield statement with the computed value, as in the following example:

iterator Items: Integer;
begin
    yield 0;
    yield 1;
    yield 2;
end;

The previous iterator always returns three integers. This is a more complicated example:

iterator Items: X;
begin
    for var i := 0 to Count - 1 do
        yield Items[i];
end;

See also

The Freya Programming Language
Type declarations
Type members
Constructors
Methods
User defined operators
Fields
Properties
Events