groovy
  1. groovy
  2. GROOVY-2904

[ [a:1, b:2, c:3] ].flatten() -> [ 1, 2, 3 ], but should be [ [a:1, b:2, c:3] ]

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.6-beta-1
    • Fix Version/s: 1.6-rc-2, 1.5.8, 1.7-beta-1
    • Component/s: None
    • Labels:
      None
    • Environment:
      Patch relative to SVN revision 12775.
    • Testcase included:
      yes
    • Patch Submitted:
      Yes
    • Number of attachments :
      3

      Description

      Updated list#flatten/set#flatten so that maps are unadulterated.

      Going with this approach means that maps can safely be used in lists. This is commonly done as a kind of "pseudobean" for unit testing – it's how I first encountered this behavior.

        Issue Links

          Activity

          Hide
          Jim White added a comment -

          I'm not talking about Groovy's whole collection type system wrt arrays. I'm saying the current implementation of flatten is bugged and it needs some sort of fix.

          That said, I'm fairly sure that the only sensible interpretation is that Groovy coerces arrays to collections. To suppose that flatten might return an array rather than a collection also means that you would have functions like collect, find, and grep return an array rather than a collection when performed on an array. That wouldn't be a very good approach because any actual implementation would have to use variable sized collections and then convert to an array at the end, which is exactly the thing that the user will do if that is what he wants.

          Show
          Jim White added a comment - I'm not talking about Groovy's whole collection type system wrt arrays. I'm saying the current implementation of flatten is bugged and it needs some sort of fix. That said, I'm fairly sure that the only sensible interpretation is that Groovy coerces arrays to collections. To suppose that flatten might return an array rather than a collection also means that you would have functions like collect, find, and grep return an array rather than a collection when performed on an array. That wouldn't be a very good approach because any actual implementation would have to use variable sized collections and then convert to an array at the end, which is exactly the thing that the user will do if that is what he wants.
          Hide
          Paul King added a comment -

          @Jim, yes very thoughtful comments. I think you have all the thinking spot on. Apologies for erring on the side of socializing such thinking behind the language in this forum. Until we have that Wings language spec in place it can't hurt to discuss things a bit ... well, not hurt too much!?

          To reiterate your last comment and reinforce the need for further work on this issue, if findAll already converts arrays to lists as shown below, why not flatten:

          def nums = 1..10 as int[]
          assert nums instanceof int[]
          def evens = nums.findAll{ it % 2 == 0 }
          assert evens == [2, 4, 6, 8, 10]
          assert evens instanceof List
          

          I think that (along with earlier examples) seems pretty convincing to me.

          Show
          Paul King added a comment - @Jim, yes very thoughtful comments. I think you have all the thinking spot on. Apologies for erring on the side of socializing such thinking behind the language in this forum. Until we have that Wings language spec in place it can't hurt to discuss things a bit ... well, not hurt too much!? To reiterate your last comment and reinforce the need for further work on this issue, if findAll already converts arrays to lists as shown below, why not flatten : def nums = 1..10 as int [] assert nums instanceof int [] def evens = nums.findAll{ it % 2 == 0 } assert evens == [2, 4, 6, 8, 10] assert evens instanceof List I think that (along with earlier examples) seems pretty convincing to me.
          Hide
          Robert Fischer added a comment -

          If arrays are going to be lists, they should implement the full list API. Having arrays be half-lists from the view of duck typing just leads to confusion, bugs, and cognitive dissonance.

          Show
          Robert Fischer added a comment - If arrays are going to be lists, they should implement the full list API. Having arrays be half-lists from the view of duck typing just leads to confusion, bugs, and cognitive dissonance.
          Hide
          Jim White added a comment -

          No worries, Paul. Some of the RC issues and a lack of sleep and other such stuff have made be a bit cranky.

          Glad to see that we have arrived at consensus.

          Show
          Jim White added a comment - No worries, Paul. Some of the RC issues and a lack of sleep and other such stuff have made be a bit cranky. Glad to see that we have arrived at consensus.
          Hide
          Paul King added a comment -

          Amended to also handle arrays as base collection type

          Show
          Paul King added a comment - Amended to also handle arrays as base collection type

            People

            • Assignee:
              Paul King
              Reporter:
              Robert Fischer
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: