scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

GroupBy in views returns wrong result in 2.9.x #4660

Closed scabug closed 13 years ago

scabug commented 13 years ago

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.

scabug commented 13 years ago

Imported From: https://issues.scala-lang.org/browse/SI-4660?orig=1 Reporter: Jesse Eichar (jesseeichar) Affected Versions: 2.9.0

scabug commented 13 years ago

Commit Message Bot (anonymous) said: (extempore in r25074) Fix for wrong results from groupBy on views. Closes #4660, no review.