Pyro

A scripting language for people who enjoy the simpler things in life.

Version 0.5.37

Iterators


Pyro's for loop uses a simple iterator protocol:

for item in [123, 456, 789] {
    echo item;
}

An object is iterable if it has an :$iter() method. This method should return an iterator — an object with a :$next() method which returns either the next item from the sequence or an err if the sequence has been exhausted.

Example

We can illustrate the iterator protocol by creating a custom Range object to iterate over a range of integers:

class Range {
    var next = 0;
    var stop;

    def $init(stop) {
        self.stop = stop;
    }

    def $iter() {
        return self;
    }

    def $next() {
        if self.next < self.stop {
            self.next += 1;
            return self.next - 1;
        }
        return $err();
    }
}

Note that in this case the :$iter() method simply returns self — a Range instance is its own iterator.

We can try out our new type like this:

for i in Range(5) {
    echo i;
}

This gives us the following output:

0
1
2
3
4

Note that Pyro already has a builtin $range() function that returns an iterator over a range of integers.

Iterator Wrappers

Pyro has a builtin function called $iter() that can wrap any iterator in a special iter type that automatically adds support for a set of chainable, lazily-evaluated utility methods.

We can try it out on our custom Range type. First, let's define some callback functions:

def is_even(n) {
    return n % 2 == 0;
}

def square(n) {
    return n * n;
}

Now we can apply them using :filter() and :map():

for i in $iter(Range(10)):filter(is_even):map(square) {
    echo i;
}

This gives us the following output:

0
4
16
36
64

All the iterators returned by Pyro's builtin types and functions come pre-wrapped in iter wrappers so we could get an identical result using the builtin $range() function without needing to call $iter():

for i in $range(10):filter(is_even):map(square) {
    echo i;
}

You can learn more about iterator wrappers here.