groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mario Garcia (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-4105) List Comprehensions are Missing from Groovy
Date Tue, 06 Feb 2018 11:38:00 GMT

    [ https://issues.apache.org/jira/browse/GROOVY-4105?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16353753#comment-16353753
] 

Mario Garcia commented on GROOVY-4105:
--------------------------------------

At the time I started to implement list comprehensions applying AST transformation to list
syntax in Groovy. I did a first attempt to see how they could look like, and this is what
I got:
{code:java}
assert [ x              | x << (1..2)]              == [1,2]
assert [ [x,y]          | x << (1..2), y << (1..2)] == [[1,1], [1,2], [2,1], [2,2]]
assert [ { x + y }      | x << (1..2), y << (1..2)] == [2, 3, 3, 4]
assert [ i              | i << [ j | j << (1..10)]] == [1, 2, 3, 4, 5, 6, 7, 8,
9, 10]
assert [[x + y , x - y] | x << (1..2), y << (1..2)] == [[2, 0], [3, -1], [3, 1],
[4, 0]]
{code}
 
 Things I didn't do:
 - *guards*: filtering elements
 - *lazyness*: take, takeWhile, skip

Although it's amazing how many things can be done with Groovy syntax + AST transformations,
it's also true, that this kind of implementation it's far from being trivial, and it should
need a more concrete proposal.
 - *What can be considered a generator*
{code:java}
x << generateValues() // a method call returning a collection of values
x << (1..20) // a range
x << [y | y << (1..10)]  // another comprehension
{code}

 - *The different expressions allowed in the value generated*

{code:java}
x // simple value
[x] // a list
{x + 1} // some value calculated in a closure
{code}

 - *What can be considered a guard*
{code:java}
{ x % 2 == 0} // a closure returning a boolean
filter(x) // a method call returning a boolean
{code}

 - *What expressions can be used to limit of elements generated*
{code:java}
[x | x << (1..10)].take(10) // following current Groovy/list method calls
[x | x << (1..10), take(10)] // methods inside the comprehension
{code}

 - *How static compilation should behave over a list comprehension*

Those are a few questions that sprang to mind but there could be other important ones that
I could have forgotten. I guess once the path is clear is a matter of time and motivation.

Another point of view, is that such feature, could be delivered by an external library so
that if it is really worth it then could be added as part of the language.

> List Comprehensions are Missing from Groovy
> -------------------------------------------
>
>                 Key: GROOVY-4105
>                 URL: https://issues.apache.org/jira/browse/GROOVY-4105
>             Project: Groovy
>          Issue Type: Improvement
>          Components: Compiler
>            Reporter: Russel Winder
>            Priority: Critical
>         Attachments: collectMany.patch
>
>
> 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.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message