diff options
5 files changed, 12 insertions, 13 deletions
diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala index 2169dcdd02..04d9002eae 100644 --- a/src/library/scala/collection/TraversableLike.scala +++ b/src/library/scala/collection/TraversableLike.scala @@ -330,18 +330,17 @@ trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr] * for which `f(x)` equals `k`. * */ - def groupBy[K](f: A => K): immutable.Map[K, Repr] = { - val m = mutable.Map.empty[K, Builder[A, Repr]] + def groupBy[K](f: A => K): Map[K, Repr] = { + var m = Map[K, Builder[A, Repr]]() for (elem <- this) { val key = f(elem) - val bldr = m.getOrElseUpdate(key, newBuilder) + val bldr = m get key match { + case None => val b = newBuilder; m = m updated (key, b); b + case Some(b) => b + } bldr += elem } - val b = immutable.Map.newBuilder[K, Repr] - for ((k, v) <- m) - b += ((k, v.result)) - - b.result + m map { case (k, b) => (k, b.result) } } /** Tests whether a predicate holds for all elements of this $coll. diff --git a/src/library/scala/collection/TraversableProxyLike.scala b/src/library/scala/collection/TraversableProxyLike.scala index f2d91ded0c..05c4c44f12 100644 --- a/src/library/scala/collection/TraversableProxyLike.scala +++ b/src/library/scala/collection/TraversableProxyLike.scala @@ -37,7 +37,7 @@ trait TraversableProxyLike[+A, +Repr <: TraversableLike[A, Repr] with Traversabl override def filterNot(p: A => Boolean): Repr = self.filterNot(p) override def collect[B, That](pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[Repr, B, That]): That = self.collect(pf)(bf) override def partition(p: A => Boolean): (Repr, Repr) = self.partition(p) - override def groupBy[K](f: A => K): immutable.Map[K, Repr] = self.groupBy(f) + override def groupBy[K](f: A => K): Map[K, Repr] = self.groupBy(f) override def forall(p: A => Boolean): Boolean = self.forall(p) override def exists(p: A => Boolean): Boolean = self.exists(p) override def count(p: A => Boolean): Int = self.count(p) diff --git a/src/library/scala/collection/TraversableViewLike.scala b/src/library/scala/collection/TraversableViewLike.scala index 9b5be82dd6..7b443e54b8 100644 --- a/src/library/scala/collection/TraversableViewLike.scala +++ b/src/library/scala/collection/TraversableViewLike.scala @@ -216,7 +216,7 @@ self => override def scanRight[B, That](z: B)(op: (A, B) => B)(implicit bf: CanBuildFrom[This, B, That]): That = newForced(thisSeq.scanRight(z)(op)).asInstanceOf[That] - override def groupBy[K](f: A => K): immutable.Map[K, This] = + override def groupBy[K](f: A => K): Map[K, This] = thisSeq.groupBy(f).mapValues(xs => newForced(xs).asInstanceOf[This]) override def stringPrefix = "TraversableView" diff --git a/src/parallel-collections/scala/collection/parallel/ParallelSeqViewLike.scala b/src/parallel-collections/scala/collection/parallel/ParallelSeqViewLike.scala index 2e9ebb1df3..40f5231fbe 100644 --- a/src/parallel-collections/scala/collection/parallel/ParallelSeqViewLike.scala +++ b/src/parallel-collections/scala/collection/parallel/ParallelSeqViewLike.scala @@ -135,7 +135,7 @@ extends SeqView[T, Coll] override def span(p: T => Boolean): (This, This) = (takeWhile(p), dropWhile(p)) override def scanLeft[S, That](z: S)(op: (S, T) => S)(implicit bf: CanBuildFrom[This, S, That]): That = newForced(thisSeq.scanLeft(z)(op)).asInstanceOf[That] override def scanRight[S, That](z: S)(op: (T, S) => S)(implicit bf: CanBuildFrom[This, S, That]): That = newForced(thisSeq.scanRight(z)(op)).asInstanceOf[That] - override def groupBy[K](f: T => K): collection.immutable.Map[K, This] = thisSeq.groupBy(f).mapValues(xs => newForced(xs).asInstanceOf[This]) + override def groupBy[K](f: T => K): collection.Map[K, This] = thisSeq.groupBy(f).mapValues(xs => newForced(xs).asInstanceOf[This]) override def +:[U >: T, That](elem: U)(implicit bf: CanBuildFrom[This, U, That]): That = newPrepended(elem).asInstanceOf[That] override def reverse: This = newReversed.asInstanceOf[This] override def patch[U >: T, That](from: Int, patch: Seq[U], replaced: Int)(implicit bf: CanBuildFrom[This, U, That]): That = newPatched(from, patch, replaced).asInstanceOf[That] diff --git a/test/files/run/colltest1.scala b/test/files/run/colltest1.scala index dd9ecdbd5e..943fe4c4e7 100644 --- a/test/files/run/colltest1.scala +++ b/test/files/run/colltest1.scala @@ -29,8 +29,8 @@ object Test extends Application { val (o, e) = ten.partition(_ % 2 == 0) assert(o.size == e.size) val gs = ten groupBy (x => x / 4) - val vs1 = (for (k <- gs.keysIterator; v <- gs(k).toIterable.iterator) yield v).toList.sorted - val vs2 = gs.values.toList.flatten.sorted + val vs1 = (for (k <- gs.keysIterator; v <- gs(k).toIterable.iterator) yield v).toList + val vs2 = gs.values.toList.flatten // val vs2 = gs.values.toList flatMap (xs => xs) assert(ten.head == 1) assert(ten.tail.head == 2) |