Python generators in Java?

R

Robert Oschler

Preamble:

- I know this is the Python forum
- I know about (and have used) Jython

I already posted this question in comp.lang.java. But after a week I have
still not received a single reply.

One of my favorite Python features is generators. I have to use Java for
one particular project (it happens). I would like to have something at
least close to Python's generators for the project.

Jython won't do because last I checked it was dormant, and has been for
quite some time. In addition, the version that it is "frozen" at (2.1)
does not contain generators (if this is wrong on any point, *please* point
me to a URL for a current version). I checked the mailing list archive and
except for a single recent post from a party external to the Jython project,
there hasn't been any mailing list activity since 2002. In fact, I saw my
two unanswered posts about Jython and generators from 2002. There was a
2.2a (alpha release) in July of 2003 with lots of warnings about using the
code in a production environment, but nothing since.

So does anyone here that has Python and Java experience have any ideas on
this?

Also, has anybody had any experience integrating CPython with Java, if it is
at all possible?

Thanks.
 
K

Kent Johnson

Robert said:
Preamble:
Jython won't do because last I checked it was dormant, and has been for
quite some time. In addition, the version that it is "frozen" at (2.1)
does not contain generators (if this is wrong on any point, *please* point
me to a URL for a current version).
Yes, that is still the current production release.

I checked the mailing list archive and
except for a single recent post from a party external to the Jython project,
there hasn't been any mailing list activity since 2002.

??? There is continuous, moderate activity on the ML. Try
http://sourceforge.net/mailarchive/forum.php?forum_id=5586

In fact, I saw my
two unanswered posts about Jython and generators from 2002. There was a
2.2a (alpha release) in July of 2003 with lots of warnings about using the
code in a production environment, but nothing since.

There has been some encouraging news about a Jython update in the works.
See the news at http://www.jython.org
We won't have generators in Jython soon but there is progress.

Kent
 
A

Alan Kennedy

[Robert Oschler]
> Preamble:
>
> - I know this is the Python forum
> - I know about (and have used) Jython
>
> I already posted this question in comp.lang.java. But after a week I
> have still not received a single reply.
>
> One of my favorite Python features is generators. I have to use Java
> for one particular project (it happens). I would like to have
> something at least close to Python's generators for the project.


Hi Robert,

Hmm, I don't see a direct question in your post, so I'll just assume
that you're generally inquiring about generator style functionality in java.

If you're looking to achieve the execution efficiency of python
generators, e.g. in terms of processing sequences one value at a time
rather than generating the entire sequence and then processing, then you
should look at java iterators, which are designed specifically for this
purpose.

Note that java 1.5 brings new syntactic features which support
iterators, e.g. short-hand for loops like this

public int sumArray(int array[]) {
int sum = 0;
for(int i : array) {
sum += i;
}
return sum;
}

Quite pythonic, IMHO.

http://java.sun.com/developer/technicalArticles/releases/j2se15langfeat/

However, if you're looking for the resumable-functions aspect of
generators, you're out of luck: java doesn't not support them. But note
that a java iterator will still very likely be more efficient than a
python generator, albeit that the code won't be as clean.

Note that there is a currently a proposal from the codehaus people to
add continuations to java, which could make both generators and full
coroutines possible in java.

http://docs.codehaus.org/display/continuation/Home

Lastly, it would probably be easier to discuss this subject if you gave
an example of the type of thing you want to do, and some reasons why you
want to do it that way. Post some python: maybe the java would be
straightforward.

Regards,
 
D

Daniel Dittmar

Robert said:
One of my favorite Python features is generators. I have to use Java for
one particular project (it happens). I would like to have something at
least close to Python's generators for the project.

Sketch:
- create a Queue class
- the 'generator' would run in a thread and put objects into the queue
(equivalent to yield)
- the 'main' program would run in the main thread and consume objects
from the queue (equivalent to calling generator.next ())
- the consumer must block if the queue is empty
- the producer must block if the number of objects in the queue exceeds
a certain amount (queue size = 1 is perfectly acceptable unless the
producer is I/O bound)
- if you use generators to walk recursive structures, then you probably
need only one queue instead of one generator for each level.
- perhaps you can get away with one superclass that creates both queue
and thread, the 'generator' could then be implemented as an anonymous class.

Daniel
 
R

Robert Oschler

Alan Kennedy said:
Hi Robert,

Hmm, I don't see a direct question in your post, so I'll just assume
that you're generally inquiring about generator style functionality in java.

If you're looking to achieve the execution efficiency of python
generators, e.g. in terms of processing sequences one value at a time
rather than generating the entire sequence and then processing, then you
should look at java iterators, which are designed specifically for this
purpose.

Note that java 1.5 brings new syntactic features which support
iterators, e.g. short-hand for loops like this

public int sumArray(int array[]) {
int sum = 0;
for(int i : array) {
sum += i;
}
return sum;
}

Quite pythonic, IMHO.

http://java.sun.com/developer/technicalArticles/releases/j2se15langfeat/

However, if you're looking for the resumable-functions aspect of
generators, you're out of luck: java doesn't not support them. But note
that a java iterator will still very likely be more efficient than a
python generator, albeit that the code won't be as clean.

Note that there is a currently a proposal from the codehaus people to
add continuations to java, which could make both generators and full
coroutines possible in java.

http://docs.codehaus.org/display/continuation/Home

Lastly, it would probably be easier to discuss this subject if you gave
an example of the type of thing you want to do, and some reasons why you
want to do it that way. Post some python: maybe the java would be
straightforward.

Regards,

Alan,

Yes it's the resumable aspect of generators I'm after. For me that is their
main appeal. I'm familiar with iterators from C++, but it's the ability to h
ave a function that maintains it's own "state" (generator) that is
attractive. It simplifies a lot of thorny programming problems.

Thanks for the link on Java continuations, I'll check that out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top