Details
-
Type:
Wish
-
Status:
Closed
-
Priority:
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.
Issue Links
- is depended upon by
-
GROOVY-762
implement syntax improvements
-
- relates to
-
GROOVY-2046
MethodClosure iterators could loop forever
-
-
GROOVY-4105
List Comprehensions are Missing from Groovy
-
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...