Tag Archives: Internal Iterator

Of internal and external iterators, and generators

Internal iterators

  • Are easy to write because you can use loops and recursion
  • Can’t be controlled by the client (except for stopping early)
  • Don’t suffer from invalidation because only one can be in operation at once (unless the container is being accessed by multiple threads)
  • Don’t need to hold on to precious resources like files and network and database connections for an unpredictable length of time
  • Can’t run in parallel, or be interleaved
  • Can only be nested by passing callbacks to callbacks

External iterators

  • Are more difficult to write because you need to maintain state and can’t use loops and recursion
  • Are controlled by the client
  • Can be run in parallel or interleaved
  • May be invalidated – you need to specify invalidation behaviour or make them robust
  • May need to hold on to precious resources like files and network and database connections for an unpredictable length of time
  • – although you can implement timeouts and lazy connects

Generators

Generators give you the best of both worlds, in the sense that they are as easy to write as internal iterators, but are exposed as external iterators.
Because they are external iterators, they can be invalidated, and can hold on to resources for an unpredictable length of time.