Skip to content

Custom Eaterator

An Eaterator mainly consists of two things:

  • T: The type of each element.
  • next(): The function that advances the iterator.

Unlike Python's built-in iterator ecosystem, eaterate takes the Rust approach, which eliminates the need for raising a StopIteration exception everytime an iterator ends.

Every next() function override should return an Option[T], which has two variants, either Some(...) (there's data) or Option.none() (there's no data, in otherwords, the iterator ended).

Let's say you want to create an iterator that says "chocolate" out loud for n times.

from eaterate import Eaterator, Option


class ChocolateEaterator(Eaterator[str]):
    n: int

    def __init__(self, n: int):
        self.n = n

    def next(self) -> Option[str]:
        if self.n <= 0:
            return Option.none()
        else:
            self.n -= 1
            return Option.some("chocolate")

# Say, 3 chocolates!
chocolates = ChocolateEaterator(n=3)

print(chocolates.next()) # Some("chocolate")
print(chocolates.next()) # Some("chocolate")
print(chocolates.next()) # Some("chocolate")
print(chocolates.next()) # Option.none()

You can also use a for block:

for item in chocolates:
    print(item)

# chocolate
# chocolate
# chocolate

You can check out the Core API to see if there's anything cool you'd like to implement. Then, submit your Pull Request (if you'd like)!

Note

You can also achieve this without writing custom Eaterator:

from eaterate import repeat

chocolates = repeat("chocolate", 3)