You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2015/06/13 07:29:00 UTC

[jira] [Comment Edited] (GROOVY-7464) Iterable.intersect() is not commutative (and allows duplicates)

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

Paul King edited comment on GROOVY-7464 at 6/13/15 5:28 AM:
------------------------------------------------------------

The original design was never meant to return just unique values and wasn't meant to be commutative. It was meant to be a shorthand for: {{ a.findAll{ !b.contains(it) } }}. Interestingly, if the input collections are of different sizes, then it is commutative but not in the way most people would expect. So, while I'm not sure that it has to return unique values and be commutative, that's what {{unique()}} could allow, I do think it needs some work.


was (Author: paulk):
The original design was never meant to return just unique values and wasn't meant to be commutative. It was meant to be a shorthand for: {{a.findAll{ !b.contains(it) } }}. Interestingly, if the input collections are of different sizes, then it is commutative but not in the way most people would expect. So, while I'm not sure that it has to return unique values and be commutative, that's what {{unique()}} could allow, I do think it needs some work.

> Iterable.intersect() is not commutative (and allows duplicates)
> ---------------------------------------------------------------
>
>                 Key: GROOVY-7464
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7464
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-jdk
>            Reporter: Peter Ledbrook
>            Assignee: Guillaume Laforge
>
> The {{intersect()}} method on {{Iterable}} has different results depending on the order of the receiver object and the argument. For example,
> {code}
> def a = [1, 1, 2, 4]
> def b = [1, 2, 2, 3]
> println a.intersect(b)
> println b.intersect(a)
> {code}
> prints out two different results ({{[1, 1, 2]}} and {{[1, 2, 2]}}).
> Assuming that the operation should be commutative, i.e. the results should be the same for both, the question then becomes whether duplicates should even be returned at all.
> I think the method should always return a set as intersection is a set operation. In other words, {{Iterable.intersect()}} becomes a convenience wrapper that converts the receiver object and the argument to sets.
> This issue started as a discussion on the [dev mailing list|http://mail-archives.apache.org/mod_mbox/incubator-groovy-dev/201506.mbox/browser].



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)