Eric Milles created GROOVY-8870:
-----------------------------------

             Summary: 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


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 spread-dot plus {{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]]
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!
{code}





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

Reply via email to