[ 
https://issues.apache.org/jira/browse/GROOVY-11127?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17745109#comment-17745109
 ] 

ASF GitHub Bot commented on GROOVY-11127:
-----------------------------------------

paulk-asert commented on PR #1915:
URL: https://github.com/apache/groovy/pull/1915#issuecomment-1644012383

   I am still keen to keep Iterable as the second argument (option 3). It makes 
most sense with how the DGM methods and the Java APIs are designed. I agree 
that non-symmetry can potentially lead to confusion but I don't see that 
applies here.
   
   If we have explained that "union" is like a Set-specific "plus", then I 
don't see why folks might expect it to work for Collections.
   If I have `Set<String> wordsSeen` and `List<String> nextSentence`, then 
doing `wordsSeen |= nextSentence` makes perfect sense just like we currently 
would allow `wordsSeen += nextSentence`. Folks dealing with large datasets 
aren't going to want the extra time or memory hit of creating the superfluous 
set. I don't see folks suddenly expecting `nextSentence |= wordsSeen` to be 
available when we have explained this as a set operation.
   
   Or, to say it a different way, Set already has `containsAll(Collection)`, 
`addAll(Collection)`,` retainAll(Collection)`, `removeAll(Collection)`, as well 
as the Collection constructor, e.g. in HashSet. I see no problem with having 
`union(Collection)` or in our case we'd have `union(Iterable)` and 
`or(Iterable)` as an alias. We already have `minus(Iterable)`, 
`plus(Iterable)`, and `intersect(Iterable)`.
   
   Much like we know that subtraction, power, division aren't symmetric and the 
order of the parameters is intrinsic to the problem domain, I see it that for 
the set-specific operators, the order is important and the iterable can be the 
second argument and have a different type to the first. Operations with a 
custom comparator aren't going to be symmetric behavior-wise in general, I 
don't see it as a given that set operations must be symmetric type-wise.
   




> Add '|', '&', and '^' operators to Set and SortedSet
> ----------------------------------------------------
>
>                 Key: GROOVY-11127
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11127
>             Project: Groovy
>          Issue Type: New Feature
>          Components: groovy-jdk
>            Reporter: Spencer Allain
>            Assignee: Paul King
>            Priority: Trivial
>             Fix For: 5.x
>
>
> 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:
> {code:java}
> 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
> {code}
> A  [Pull Request|https://github.com/apache/groovy/pull/1915] 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.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to