Return-Path: X-Original-To: apmail-groovy-dev-archive@minotaur.apache.org Delivered-To: apmail-groovy-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5922C17DF0 for ; Thu, 11 Jun 2015 14:32:29 +0000 (UTC) Received: (qmail 40409 invoked by uid 500); 11 Jun 2015 14:32:29 -0000 Delivered-To: apmail-groovy-dev-archive@groovy.apache.org Received: (qmail 40365 invoked by uid 500); 11 Jun 2015 14:32:29 -0000 Mailing-List: contact dev-help@groovy.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.incubator.apache.org Delivered-To: mailing list dev@groovy.incubator.apache.org Received: (qmail 40355 invoked by uid 99); 11 Jun 2015 14:32:28 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 11 Jun 2015 14:32:28 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of blackdrag@gmx.org designates 212.227.17.20 as permitted sender) Received: from [212.227.17.20] (HELO mout.gmx.net) (212.227.17.20) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 11 Jun 2015 14:30:14 +0000 Received: from [192.168.1.3] ([85.180.45.51]) by mail.gmx.com (mrgmx102) with ESMTPSA (Nemesis) id 0MQMBU-1Ybejm22Qe-00Tp7b for ; Thu, 11 Jun 2015 16:32:02 +0200 Message-ID: <55799CD0.4000705@gmx.org> Date: Thu, 11 Jun 2015 16:36:00 +0200 From: Jochen Theodorou User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: dev@groovy.incubator.apache.org Subject: Re: intersect() returns duplicate elements References: <5577C0DB.2080207@gmx.org> <5577E3D9.7050807@gmx.org> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:1u/m83CfB7kg1IqMCL9J8EdVq0q0eIxahoEGSSe3x6rNBhHglx1 72Zf0ZBTXjkMNj8QbKr98hs/Om28u44dJPzv+sXmWudG4MT3F4l9wKoi9fVgG+55G2y8Njv d0cKNCJSgACQlh4F0r0QoTtimrX44SsUMSp/m5HLrDKi6KAlXkanah7ZhQz8flGT+7XbgKR chcgqO99EtSi2kYwRInRA== X-UI-Out-Filterresults: notjunk:1; X-Virus-Checked: Checked by ClamAV on apache.org Am 11.06.2015 14:32, schrieb Peter Ledbrook: > If I define intersect like this: > A intersect B = C: > x element of A and x element of B is equivalent to x element of C > > with A,B,C not being Sets, just similar, then I don't see why repetition > should not be allowed in the result. If somebody wants no repetition, > then he should use sets or unique. > > > I have a different take on this. I think intersect() is by definition a > set operation and the method on Iterable is simply a convenience that > automatically converts the object and the arguments to sets. I would > even argue for the result of the method always being a set as well. If you argue like that I have to ask: Does it make sense to convert an Iterable to Set automatically? Wouldn't it be better to instead have it on Set only? > Even if one doesn't agree with this definition, the current > implementation is broken. Surely intersect() must be a commutative > operation? And yet this code > > def a = [1, 1, 2, 4] > def b = [1, 2, 2, 3] > a.intersect(b) > > produces a result of [1, 2, 2]. b.intersect(a) produces [1, 1, 2]. you are right: https://proofwiki.org/wiki/Intersection_is_Commutative I tried using the history of the method to find out why it was added this way, but this code goes back to 2003, and I really never talked to the author of this method. I did see that the early version did use a TreeSet in some cases, but not in all cases. I really wonder if we should keep the method on anything that is no Set... actually even with sets... If a non-standard comparator is used, problems like above will easily happen again. My feeling is that this was added to Collection (now Iterable I know, but back then Collection), because there is no native construct for sets. bye blackdrag -- Jochen "blackdrag" Theodorou blog: http://blackdragsview.blogspot.com/