jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • groovy
  • GROOVY-54

Add generator syntax similar to Python's

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Wish Wish
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Won't Fix
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

Generators (like list comprehensions but without the list) rock. We need to add something like this to the language...

e.g.

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

  • Options
    • Sort By Name
    • Sort By Date
    • Ascending
    • Descending
    • Download All

Attachments

  1. Java Source File
    Generator.java
    12/Apr/04 8:24 AM
    2 kB
    Stepan Koltsov

Issue Links

is depended upon by

Task - A task that needs to be done. GROOVY-762 implement syntax improvements

  • Major - Major loss of function.
  • Closed - The issue is considered finished, the resolution is correct. Issues which are not closed can be reopened.
relates to

Bug - A problem which impairs or prevents the functions of the product. GROOVY-2046 MethodClosure iterators could loop forever

  • Minor - Minor loss of function, or other problem where easy workaround is present.
  • Open - The issue is open and ready for the assignee to start work on it.

Improvement - An improvement or enhancement to an existing feature or task. GROOVY-4105 List Comprehensions are Missing from Groovy

  • Critical - Crashes, loss of data, severe memory leak.
  • Open - The issue is open and ready for the assignee to start work on it.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
Stepan Koltsov added a comment - 12/Apr/04 8:20 AM

Is anybody alive here? I tried to comment other issues but didn't get any answers

Comment for this issue: as far as I understand, generators must be executed in another stack (not current). The one way to create new stack in Java is to create Thread. So generators should be executed in another thread. So code like:

def f() {
for (i: 0..3) { yield factorial(i); }
}

should be compiled to something like:

class Generator implements Iterator {
yieldValue;

next() {
if (!hasNext())
throw new NoSuchElementException();
synchronized (thread) { returnValue = yieldValue; thread.notify(); // object has been read return returnValue; }
}

hasNext() { ... }

class GeneratorThread extends Thread {
synchornized run() {
for (i: 0..3) { yieldValue = factorial(i); thread.wait(); // wait object has been read }
}
}

GeneratorThread thread;

Generator() { thread = new GeneratorThread(); thread.start(); }
}

def f() {
return new Generator();
}

Real code is harder to understand. I'll attach working Generator class, written in Java in 5 minutes.

I'm right? So I'd like to implements this in Groovy...

Show
Stepan Koltsov added a comment - 12/Apr/04 8:20 AM Is anybody alive here? I tried to comment other issues but didn't get any answers Comment for this issue: as far as I understand, generators must be executed in another stack (not current). The one way to create new stack in Java is to create Thread. So generators should be executed in another thread. So code like: def f() { for (i: 0..3) { yield factorial(i); } } should be compiled to something like: class Generator implements Iterator { yieldValue; next() { if (!hasNext()) throw new NoSuchElementException(); synchronized (thread) { returnValue = yieldValue; thread.notify(); // object has been read return returnValue; } } hasNext() { ... } class GeneratorThread extends Thread { synchornized run() { for (i: 0..3) { yieldValue = factorial(i); thread.wait(); // wait object has been read } } } GeneratorThread thread; Generator() { thread = new GeneratorThread(); thread.start(); } } def f() { return new Generator(); } Real code is harder to understand. I'll attach working Generator class, written in Java in 5 minutes. I'm right? So I'd like to implements this in Groovy...
Hide
Permalink
Stepan Koltsov added a comment - 12/Apr/04 8:24 AM

I've posted Generator.java – source of Generator class.

class Main {
public static void main(String[] args) throws Exception {
for (Iterator i = new Generator(); i.hasNext(); ) { System.out.println(i.next()); }
}
}

to test Generator class.

Show
Stepan Koltsov added a comment - 12/Apr/04 8:24 AM I've posted Generator.java – source of Generator class. class Main { public static void main(String[] args) throws Exception { for (Iterator i = new Generator(); i.hasNext(); ) { System.out.println(i.next()); } } } to test Generator class.
Hide
Permalink
Stepan Koltsov added a comment - 16/Apr/04 1:39 PM

My previous comments are wrong: I wrote about "simple generators", not "generator expressions".

Show
Stepan Koltsov added a comment - 16/Apr/04 1:39 PM My previous comments are wrong: I wrote about "simple generators", not "generator expressions".
Hide
Permalink
Tom Popovich added a comment - 08/Oct/07 4:36 PM

Actually list comprehensions also rock, in python you can do the following

[ (x,y) for x in range(5) for y in range(3) if (y+x) % (x+2) == 0 ]

This just an example of the syntax, and it runs x and y independently and the optional IF part
filters when the result is generated,
in python the above gives: [(0, 0), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2)] as a result.

[[ The concept is borrowed from lisp and haskel -> in Lisp you have a loop construct that is similar where by design it runs differently ,
instead of a cross product, it will pulls values out of the sequences in parallel and will stop running as soon as one sequence runs out,
which might also be useful in practice, see below for an example. ]]

Another python example: a cross product is easily seen here:

[ (x,y) for x in range(3) for y in range(3) ] ==> [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

==========
some lisp code to also show a sequencial read in a comprehension-like generation; you can run the code in XEmacs (or Gnu Emacs) scratch buffer

(loop for x in '( 0 1 2 3 )
for y in '( 0 1)
collect (list x y) )

==> yields this as a value ( (0 0) (1 1) )

The generation stopped as soon as y ran out.

This acts like a ZIP operation in python: E.g.

zip( range(4), range(2) ) ==> yields [(0, 0), (1, 1)]

Show
Tom Popovich added a comment - 08/Oct/07 4:36 PM Actually list comprehensions also rock, in python you can do the following [ (x,y) for x in range(5) for y in range(3) if (y+x) % (x+2) == 0 ] This just an example of the syntax, and it runs x and y independently and the optional IF part filters when the result is generated, in python the above gives: [(0, 0), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2)] as a result. [[ The concept is borrowed from lisp and haskel -> in Lisp you have a loop construct that is similar where by design it runs differently , instead of a cross product, it will pulls values out of the sequences in parallel and will stop running as soon as one sequence runs out, which might also be useful in practice, see below for an example. ]] Another python example: a cross product is easily seen here: [ (x,y) for x in range(3) for y in range(3) ] ==> [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] ========== some lisp code to also show a sequencial read in a comprehension-like generation; you can run the code in XEmacs (or Gnu Emacs) scratch buffer (loop for x in '( 0 1 2 3 ) for y in '( 0 1) collect (list x y) ) ==> yields this as a value ( (0 0) (1 1) ) The generation stopped as soon as y ran out. This acts like a ZIP operation in python: E.g. zip( range(4), range(2) ) ==> yields [(0, 0), (1, 1)]
Hide
Permalink
Guillaume Laforge added a comment - 08/Oct/07 4:54 PM

I'm scheduling it for discussion for Groovy 2.0

Show
Guillaume Laforge added a comment - 08/Oct/07 4:54 PM I'm scheduling it for discussion for Groovy 2.0
Hide
Permalink
Karl Pietrzak added a comment - 01/Mar/11 9:41 PM

I can't wait for list comprehensions, Groovy 2.0 or before!

Show
Karl Pietrzak added a comment - 01/Mar/11 9:41 PM I can't wait for list comprehensions, Groovy 2.0 or before!
Hide
Permalink
blackdrag blackdrag added a comment - 20/May/11 9:15 AM

We decided to clean JIRA from some old issues and instead move them to a page collecting these ideas to be fleshed out. This doesn't mean we don't want this idea, it just means this should be dopne using a GEP. This issue can still be reopened. IF you really want this feature, please make a GEP.

See http://docs.codehaus.org/display/GROOVY/Miscellaneous+Feature+Requests+and+Enhancements
See http://docs.codehaus.org/display/GroovyJSR/Groovy+Enhancement+Proposal

Show
blackdrag blackdrag added a comment - 20/May/11 9:15 AM We decided to clean JIRA from some old issues and instead move them to a page collecting these ideas to be fleshed out. This doesn't mean we don't want this idea, it just means this should be dopne using a GEP. This issue can still be reopened. IF you really want this feature, please make a GEP. See http://docs.codehaus.org/display/GROOVY/Miscellaneous+Feature+Requests+and+Enhancements See http://docs.codehaus.org/display/GroovyJSR/Groovy+Enhancement+Proposal

People

  • Assignee:
    blackdrag blackdrag
    Reporter:
    james strachan
Vote (7)
Watch (6)

Dates

  • Created:
    16/Nov/03 6:26 PM
    Updated:
    20/May/11 9:16 AM
    Resolved:
    20/May/11 9:16 AM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.