Python generators in Java?

Discussion in 'Python' started by Robert Oschler, Nov 15, 2004.

  1. 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.

    --
    Robert Oschler
    http://www.robotsrule.com/phpBB2/
    Robot & Android Discussion Forum
     
    Robert Oschler, Nov 15, 2004
    #1
    1. Advertising

  2. Robert Oschler

    Kent Johnson Guest

    Robert Oschler wrote:
    > 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
     
    Kent Johnson, Nov 15, 2004
    #2
    1. Advertising

  3. Robert Oschler

    Alan Kennedy Guest

    [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,

    --
    alan kennedy
    ------------------------------------------------------
    email alan: http://xhaus.com/contact/alan
     
    Alan Kennedy, Nov 15, 2004
    #3
  4. Robert Oschler wrote:
    > 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
     
    Daniel Dittmar, Nov 15, 2004
    #4
  5. "Alan Kennedy" <> wrote in message
    news:I%4md.41892$...
    >
    > 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 kennedy
    > ------------------------------------------------------
    > email alan: http://xhaus.com/contact/alan


    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.

    --
    Robert Oschler
    http://www.robotsrule.com/phpBB2/
    Robot & Android Discussion Forum
     
    Robert Oschler, Nov 15, 2004
    #5
  6. On Mon, 15 Nov 2004 16:44:46 +0000, rumours say that Alan Kennedy
    <> might have written:

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


    In that page, under "Autoboxing/Unboxing", there is the following
    sentence:

    """
    In a pure object-oriented language there should be no difference between
    a primitive data type and a reference, as everything is an object.
    """

    How sweet... a Sun document for Java that can be used for Python
    advocacy: "Python is a pure object-oriented language" :)
    --
    TZOTZIOY, I speak England very best,
    "Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
     
    Christos TZOTZIOY Georgiou, Nov 18, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. anton muhin

    Python parser generators

    anton muhin, Nov 28, 2003, in forum: Python
    Replies:
    2
    Views:
    326
    Dave Kuhlman
    Nov 28, 2003
  2. The_Incubator
    Replies:
    4
    Views:
    435
    Linus Elman
    Jan 6, 2004
  3. Mikey At Work

    Python HTML Documentation Generators

    Mikey At Work, Feb 24, 2004, in forum: Python
    Replies:
    3
    Views:
    386
    Brad Clements
    Feb 25, 2004
  4. David Stockwell
    Replies:
    3
    Views:
    395
    Peter Otten
    May 13, 2004
  5. Tom Sheffler

    generators in Java?

    Tom Sheffler, Dec 30, 2005, in forum: Python
    Replies:
    2
    Views:
    305
    Dave Benjamin
    Jan 2, 2006
Loading...

Share This Page