Scala Programming Language
  1. Scala Programming Language
  2. SI-4660

GroupBy in views returns wrong result in 2.9.x

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.9.0
    • Fix Version/s: Scala 2.9.1
    • Component/s: Collections
    • Labels:

      Description

      The following test fails (It works in 2.8.1.final):

       
      val traversable = 1 to 30 map {_.toString}
      def normalize(m:Map[Char,Traversable[String]]) = m.map{case (k,v) => (k,v.toList)}
      val groupedFromView = (traversable view).groupBy(_(0))
      val groupedFromStrict = traversable.groupBy(_(0))
      
      assert(normalize(groupedFromView) == normalize(groupedFromStrict))
      

      groupBy throws an exception in 2.9.0 but in 2.9.0.1 just returns the wrong value.

      Essentially what is happening is the groups are formed correctly but the values in each group are always the entire collection. I believe the bug is in TraversableViewLike:170. Look at the implementation:

       
        override def groupBy[K](f: A => K): immutable.Map[K, This] =
          thisSeq.groupBy(f).mapValues(xs => newForced(thisSeq))
      

      The section newForced(thisSeq) probably should be newForced(xs).

      I have tried this in a subclass and the fix worked.

        Activity

        Hide
        Commit Message Bot added a comment -

        (extempore in r25074) Fix for wrong results from groupBy on views. Closes #4660, no review.

        Show
        Commit Message Bot added a comment - (extempore in r25074 ) Fix for wrong results from groupBy on views. Closes #4660, no review.

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Jesse Eichar
          • Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development