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

Add '|', '&', and '^' operators to Set and SortedSet

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • None
    • 5.0.0-alpha-1
    • groovy-jdk
    • None

    Description

      Many languages conventionally allow sets to use '|' as union, '&' as intersection, and '^' as symmetric difference operations on sets.

      This ticket is proposing adding these operations as DefaultGroovyMethods for Set and SortedSet such that the below tests should pass:

      Set a = [1,2,3,4] as Set
      Set b = [3,4,5,6] as Set
      
      assert (a | b) == [1,2,3,4,5,6] as Set
      assert (a & b) == [3,4] as Set
      assert (a ^ b) == [1,2,5,6] as Set
      
      Set d = ['a', 'B', 'c'] as Set
      Set e = ['A', 'b', 'D'] as Set
      
      assert d.and(e, String.CASE_INSENSITIVE_ORDER) == ['a', 'B'] as Set
      assert d.and(e, Comparator.naturalOrder()) == [] as Set
      assert d.xor(e, String.CASE_INSENSITIVE_ORDER) == ['c', 'D'] as Set
      assert d.xor(e, Comparator.naturalOrder()) == ['a', 'B', 'c', 'A', 'b', 'D'] as Set
      

      A Pull Request exists that implements the desired additions for the 5.x groovy branch (master), but it should be fairly easy to make the functionality available in 4.x if desired.

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              merscwog Spencer Allain
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: