text: 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
  Activity
  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.
  My previous comments are wrong: I wrote about "simple generators", not "generator expressions".
  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)]
  I'm scheduling it for discussion for Groovy 2.0
  I can't wait for list comprehensions, Groovy 2.0 or before!
  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
  See
  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;
  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...