There has been some discussions lately about the need to support generators now that Virtual threads are a thing. I am reading up some examples and I still don't understand what is a generator and why is it powerful.
Some examples from Python: https://www.programiz.com/python-programming/generator
Some examples using plain Java 8: https://www.mguenther.net/2016/01/functional_generators_in_java_8/index.html
I am a little confused why this is regarded as a powerful pattern. It seems I can implement the same using a normal `Iterator`. For instance, from the Python example:
class PowTwoGen {
final int max;
int n = 0;
PowTwoGen(int max) { this.max = max;}
boolean hasNext() { return n <= max;}
int next() { return Math.pow(2, n++); }
}
The examples for the Java 8 article can also be done with a simple method call but instead the examples with the Generator seems overly complex. What am I missing?
Somebody could make the case that returning a sentinel value or an exception is a better API since there is no risk somebody else is going to call the next() method after you call hasNext() and next(). Writing a generator that wraps a generator is a little simpler than writing an interest or that wraps an iteration because you don’t have to write a hasNext() function, which can occasionally be awkward.
That generator library has a few functions, like map that work on generators, unfortunately the Java stdlib doesn’t come with anything like that. (There is the streams API but it is over-complicated.)
I’ll point out this library I wrote
https://github.com/paulhoule/pidove
which does a lot of what the Steams library does but it works on iterators without creating streams. If you like those generator examples you might like pidove.
As for Python it is kinda accidental that generators would up related to coroutines, that is, generators were an easy way to implement coroutines, later async/await and stuff like that got added.