Walking the Route
Walking the Route: Iterator: Next Stop, Please, Generators: Iterators the Modern Way.
Updated
- Iterator: Next Stop, Please (Journeyman): When every piece of code that walks a route has its own loop over how the route is stored, changing the storage breaks all of them; an iterator hands out the stops one at a time, so callers ask for the next stop and never learn whether it came from a list, zones or a server.
- Generators: Iterators the Modern Way (Journeyman): A hand-written iterator is a bookmark and a page of bookkeeping; a generator writes the same iterator as an ordinary loop that hands out each item with yield, pausing until the next one is asked for, so stops can even be fetched from a server only when they're needed.