groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul King (JIRA)" <j...@apache.org>
Subject [jira] [Closed] (GROOVY-8870) Spread-dot operator on list of lists
Date Thu, 13 Dec 2018 10:05:01 GMT

     [ https://issues.apache.org/jira/browse/GROOVY-8870?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Paul King closed GROOVY-8870.
-----------------------------

> Spread-dot operator on list of lists
> ------------------------------------
>
>                 Key: GROOVY-8870
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8870
>             Project: Groovy
>          Issue Type: Documentation
>            Reporter: Eric Milles
>            Assignee: Paul King
>            Priority: Minor
>             Fix For: 3.0.0-alpha-4, 2.5.4
>
>
> I'm not sure if this is intended, but I ran into some nice/useful behavior of the spread-dot
operator when applying to a list of lists.  I was trying to use {{collectMany}} and found
that I would need to do an inner {{collectMany}} or switch to {{inject}}.  But it turns out
spread-dot and {{flatten}} handled the situation very nicely.
> Could you please add a note on this to the documentation (section 1.2.8 -- other operators)?
 I don't think it is common knowledge that spread-dot does not always gather up results in
a flat list.
> {code:groovy}
> class Foo {
>   String thing
> }
> class Bar {
>   List<Foo> foos
> }
> class Baz {
>   List<Bar> bars
> }
> List<Foo> f = [new Foo(thing:'1'), new Foo(thing:'2')]
> assert f*.thing == ['1','2']
> List<Bar> b = [new Bar(foos:f), new Bar(foos:f)]
> assert b*.foos*.thing == [['1','2'], ['1','2']]
> assert b*.foos*.thing.flatten()​ == ['1','2','1','2']
> // this was my use case:
> List<Baz> z = [new Baz(bars:b), new Baz(bars:b)]
> ​assert z*.bars*.foos == [[f, f], [f, f]] // each f is a list of 2 Foos
> assert z*.bars*.foos*.thing == [[['1','2'], ['1','2']], [['1','2'], ['1','2']]]
> assert z*.bars*.foos*.thing.flatten()​ == ['1','2','1','2','1','2','1','2'] // for
the win!
> // in this case I could have also written it without the stars:
> assert z.bars.foos.thing.flatten()​ == ['1','2','1','2','1','2','1','2']
> {code}



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

Mime
View raw message