Ask HN: What problems do Generators solve in Java?
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?