generators in Java?

Dave Benjamin ramen at lackingtalent.com
Mon Jan 2 19:49:12 CET 2006


On Fri, 30 Dec 2005, Tom Sheffler wrote:

> Does Java have generators?  I am aware of the "Iterator" interface,
> but it seems much more restrictive.  Python generators are useful
> for many more things than simply list enumeration, but the Java
> Iterator seems limited.

What makes you think that Java's Iterators are more restrictive or 
limited? As far as I understand, Java's hasNext/next protocol is 
essentially the same as Python's next/StopIteration protocol. The main 
advantage of the use of generators is that, when converting from a 
callback-style or non-streaming function to one that produces values 
on-demand, you don't have to rewrite your function to save intermediate 
states (which can be a considerable amount of work).

A good explanation of the rationale for generators in Python is in the 
"Motivation" section of PEP 255:

http://www.python.org/peps/pep-0255.html

-- 
  .:[ dave benjamin: ramen/[spoomusic.com] ]:.



More information about the Python-list mailing list