Generators

ThoughtStorms Wiki

Never used a generator in anger before. But I came across them in SimPy so I figure I needed to get up to speed.

Basically a generator is like a static function which can yield a value at any point, and holds onto where it is during execution, rather than just it's local variables.

So basically you can set one going, and periodically poll it to run a bit more until it returns it's next value.

Back when I was an idiot I wrote the following. And fortunately AdrianHoward schooled me

From my perspective, what's exciting is that this is basically a criticism of the standard conventions of boundary drawing in code. It's an admission that there are times when it's inconvenient to apply the function-as-stateless-black-box as a tool for organizing code. It overturns many automatic assumptions we make about modularizing software systems.

The "standard conventions"? Whose standard? Some of us have been quite happy with [coroutines http://en.wikipedia.org/wiki/Coroutine coroutines]] (what the rest of the world calls generators) for years :-)

You'll find them in Simula, Module-2, Lisp, Perl, Ruby and many other languages.

AdrianHoward

Hmmm, really it's a kind of EscapementMechanism.