Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4885

groupBy with a list or an array of closures as it's parameter for nested grouping

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.8.1, 1.9-beta-1
    • Component/s: groovy-jdk
    • Labels:
      None
    • Flags:
      Patch

      Description

      As described in a thread in the groovy user list here:

      http://groovy.329449.n5.nabble.com/a-possibly-useful-collection-method-tp4428105p4428105.html

      An implementation of groupBy that receives a list or an array of closures as criteria for grouping would be useful for making nested groupings.

      def grouped = [
          [aa: 11, bb: 22, cc: 33],
          [aa: 22, bb: 22, cc: 44],
          [aa: 11, bb: 22, cc: 55],
          [aa: 22, bb: 33, cc: 66],
          [aa: 33, bb: 44, cc: 77]
      ].groupBy({ it.aa }, { it.bb })
      
      def expected = [
          11:[22:[[aa:11, bb:22, cc:33], [aa:11, bb:22, cc:55]]],
          22:[22:[[aa:22, bb:22, cc:44]], 33:[[aa:22, bb:33, cc:66]]],
          33:[44:[[aa:33, bb:44, cc:77]]]
      ]
      
      assert grouped[11][22] == [[aa:11, bb:22, cc:33], [aa:11, bb:22, cc:55]]
      assert grouped[22][33] == [[aa:22, bb:33, cc:66]]
      assert grouped[11][99] == null
      

      groupBy with one closure criterion would behave like the current implementation of groupBy.

        Attachments

          Activity

            People

            • Assignee:
              Guillaume Guillaume Delcroix
              Reporter:
              dsrkoc Dinko Srkoc
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: