Groovy
  1. Groovy
  2. GROOVY-4869

Add ability to 'slice' Map, querying multiple keys at once

    Details

    • Type: New Feature New Feature
    • Status: Reopened
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: groovy-jdk
    • Labels:
      None
    • Flags:
      Patch

      Description

      It was brought up on the dev mailing list that you could query multiple list values at once, but not multiple Map values:

      Message from groovy-dev by Jon Cox

      Groovy currently lets you say:

      def    x       =  [100,200,300,400]
      assert x[0]    == 100
      assert x[2]    == 300
      assert x[0,2]  == [100,300]
      

      You can say:

      def    y        =  [moo: 100, cow: 200, egg:300 hen:400]
      assert y["moo"] == 100
      assert y["cow"] == 300
      

      But currently, you cannot slice a Map like this:

      assert y["moo","egg"] == [100,300] 
      

      This patch adds that functionality, so you can do (from the unit test)

          void testMapSlice() {
              def m = [ a:1, b:2, c:3 ]
              
              assert m[ 'a', 'b' ] == [ 1, 2 ]
              assert m[ 'a', 'c' ] == [ 1, 3 ]
              assert m[ 'a', 'd', 'c' ] == [ 1, null, 3 ]
          }
      

      Note that the resultant List contains null for keys that were not found. This (I believe) differs from the way that Perl handles this (I believe they are just skipped in perl), but I think that having the null values gives the developer more information.

      And they could be filtered out by doing something like:

      m[ 'a', 'd', 'c' ].findAll { it }
      

      Hope it's ok!

      1. map_slice.diff
        2 kB
        Tim Yates
      2. slice_instead_of_getAt.diff
        2 kB
        Tim Yates

        Activity

        Tim Yates created issue -
        Tim Yates made changes -
        Field Original Value New Value
        Attachment slice_instead_of_getAt.diff [ 55500 ]
        Tim Yates made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Won't Fix [ 2 ]
        Tim Yates made changes -
        Status Resolved [ 5 ] Reopened [ 4 ]
        Resolution Won't Fix [ 2 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12733796 ] Default workflow, editable Closed status [ 12745620 ]
        Mark Thomas made changes -
        Patch Submitted Yes [ 10763 ]
        Flags Patch [ 10430 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12969062 ] Default workflow, editable Closed status [ 12976808 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Tim Yates
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:

              Development