Generator (computer programming): Difference between revisions

Content deleted Content added
Line 310:
Even simple iterators built this way tend to be significantly bulkier than those using generators, with a lot of [[boilerplate code]].
 
The original example above could be written in '''Java 7''' as:
<source lang="Java">
// Iterator implemented as anonymous class. This uses generics but doesn't need to.
Line 340:
</source>
 
An infinite Fibonacci sequence could also be written int '''Java 7''' as an Iterator:
<source lang="java">
Iterable<Integer> fibo = new Iterable<Integer>() {
Line 378:
 
 
Also the Fibonacci sequence could also be written using '''java 8''' Stream interface:
<source lang="java">
Stream.generate(new Supplier<Integer>() {
int a = 1;
int b = 2;
 
public Integer get() {
int temp = a;
Line 393 ⟶ 392:
</source>
 
Or get an Iterator from the '''Java 8''' super-interface BaseStream of Stream interface.
<source lang="java">
public Iterable<Integer> fibonacci(int limit){