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

Iterable.intersect() is not commutative (and allows duplicates)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Won't Fix
    • 2.4.3
    • None
    • groovy-jdk
    • None

    Description

      The intersect() method on Iterable has different results depending on the order of the receiver object and the argument. For example,

      def a = [1, 1, 2, 4]
      def b = [1, 2, 2, 3]
      
      println a.intersect(b)
      println b.intersect(a)
      

      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.

      Attachments

        Activity

          People

            paulk Paul King
            pledbrook Peter Ledbrook
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: