groovy
  1. groovy
  2. GROOVY-4105

List Comprehensions are Missing from Groovy

    Details

    • Type: Improvement Improvement
    • Status: Open Open
    • Priority: Critical Critical
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Compiler
    • Labels:
      None
    • Number of attachments :
      1

      Description

      List comprehensions are present in C#, Clojure, Common Lisp, Erlang, Haskell, JavaScript, Boo, OCaml, Perl, Python, Scala, Scheme and other languages. They are obviously missing from Groovy.

        Issue Links

          Activity

          Hide
          Paul King added a comment - - edited

          A collectMany() method for DGM is now attached as a patch. No lazy loading anywhere to be seen but might allow exploration from the LINQ-like syntax point of view if anyone has interest.

          Show
          Paul King added a comment - - edited A collectMany() method for DGM is now attached as a patch. No lazy loading anywhere to be seen but might allow exploration from the LINQ-like syntax point of view if anyone has interest.
          Hide
          Russel Winder added a comment - - edited

          I'll stick with wanting something like:

          [ ( x , y ) | x <- [ 0 .. 4 ] , y <- [ 0 .. 2 ] , ( y + x ) `mod` ( x + 2 ) == 0 ] -- Haskell
          [ { X , Y } || X <- [ 0 , 1 , 2 , 3 , 4 ] , Y <- [ 0 , 1 , 2 ] ,  ( Y + X ) rem ( X + 2 ) == 0 ] % Erlang
          [ ( x , y ) for x in range(5) for y in range(3) if ( y + x ) % ( x + 2 ) == 0 ]  # Python
          
          Show
          Russel Winder added a comment - - edited I'll stick with wanting something like: [ ( x , y ) | x <- [ 0 .. 4 ] , y <- [ 0 .. 2 ] , ( y + x ) `mod` ( x + 2 ) == 0 ] -- Haskell [ { X , Y } || X <- [ 0 , 1 , 2 , 3 , 4 ] , Y <- [ 0 , 1 , 2 ] , ( Y + X ) rem ( X + 2 ) == 0 ] % Erlang [ ( x , y ) for x in range(5) for y in range(3) if ( y + x ) % ( x + 2 ) == 0 ] # Python
          Hide
          blackdrag blackdrag added a comment -

          Firstly, I don't think this issue should be critical. List comprehensions... the origin is the mathematic way to define an set or relation. In the best case this is not done recursively. As far as I can see the Haskell and the Erlang version define a true/false valued list. The Python example... I don't know, looks like it does something else - the "if" is irritating me.

          alternatives:

          [{x,y -> x in 0..<5 && y in x...x+1}, {x,y -> (y+x) % (x+2) == 0}] as ListComprehension
          {def (x,y),  x in 0..<5 && y in x...x+1,  (y+x) % (x+2) == 0 } as ListComprehension
          
          ListComprehension.make { x,y ->
            range x : 0..<5
            range y : x...x+1
            value { (y+x) % (x+2) == 0 }
          }
          
          Show
          blackdrag blackdrag added a comment - Firstly, I don't think this issue should be critical. List comprehensions... the origin is the mathematic way to define an set or relation. In the best case this is not done recursively. As far as I can see the Haskell and the Erlang version define a true/false valued list. The Python example... I don't know, looks like it does something else - the "if" is irritating me. alternatives: [{x,y -> x in 0..<5 && y in x...x+1}, {x,y -> (y+x) % (x+2) == 0}] as ListComprehension {def (x,y), x in 0..<5 && y in x...x+1, (y+x) % (x+2) == 0 } as ListComprehension ListComprehension.make { x,y -> range x : 0..<5 range y : x...x+1 value { (y+x) % (x+2) == 0 } }
          Hide
          Paul King added a comment -

          Useful pointer to the LINQ transformation rules (if we go that way):
          http://blogs.msdn.com/wesdyer/archive/2006/12/21/comprehending-comprehensions.aspx

          Show
          Paul King added a comment - Useful pointer to the LINQ transformation rules (if we go that way): http://blogs.msdn.com/wesdyer/archive/2006/12/21/comprehending-comprehensions.aspx
          Hide
          The Alchemist added a comment -

          I actually like the 'if' in Python list comprehensions; it fits very well in the Python language (e.g., use 'and' instead of '&&' for boolean AND).

          And it makes the code way more readable, IMHO.

          Perhaps we can compromise on many temporary lists for now and transitioning to lazy lists in Groovy 2? I say, let the JVM do what it does well and GC those lists.

          Show
          The Alchemist added a comment - I actually like the 'if' in Python list comprehensions; it fits very well in the Python language (e.g., use 'and' instead of '&&' for boolean AND). And it makes the code way more readable, IMHO. Perhaps we can compromise on many temporary lists for now and transitioning to lazy lists in Groovy 2? I say, let the JVM do what it does well and GC those lists.

            People

            • Assignee:
              Unassigned
              Reporter:
              Russel Winder
            • Votes:
              2 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated: