Skip to content

to(Seq) doesn't evaluate collection when called on a Seq #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,75 @@

package scala.collection.compat

import scala.collection.LinearSeq
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.Builder
import scala.collection.{immutable => i, mutable => m}

/* builder optimized for a single ++= call, which returns identity on result if possible
* and defers to the underlying builder if not.
*/
private final class IdentityPreservingSeqBuilder[A](that: Builder[A, Seq[A]])
extends Builder[A, Seq[A]] {
var collection: Seq[A] = null
var ruined = false

final override def ++=(elems: TraversableOnce[A]): this.type =
if(!ruined && collection == null && elems.isInstanceOf[Seq[_]]) {
collection = elems.asInstanceOf[Seq[A]]
this
}
else {
ruined = true
if (collection != null) that ++= collection
that ++= elems
collection = null
this
}

final def +=(elem: A): this.type = {
collection = null
ruined = true
that += elem
this
}
final def clear(): Unit = {
collection = null
if (ruined) that.clear()
}
final def result(): Seq[A] = if(ruined) that.result() else if (collection eq null) Nil else collection
}

private final class IdentityPreservingLinearSeqBuilder[A](that: Builder[A, LinearSeq[A]]) extends Builder[A, LinearSeq[A]] {
var collection: LinearSeq[A] = null
var ruined = false

final override def ++=(elems: TraversableOnce[A]): this.type =
if(!ruined && collection == null && elems.isInstanceOf[LinearSeq[_]]) {
collection = elems.asInstanceOf[LinearSeq[A]]
this
}
else {
ruined = true
if (collection != null) that ++= collection
that ++= elems
collection = null
this
}

final def +=(elem: A): this.type = {
collection = null
ruined = true
that += elem
this
}
final def clear(): Unit = {
collection = null
if (ruined) that.clear()
}
final def result(): LinearSeq[A] = if(ruined) that.result() else if (collection eq null) Nil else collection
}

private[compat] object CompatImpl {
def simpleCBF[A, C](f: => Builder[A, C]): CanBuildFrom[Any, A, C] = new CanBuildFrom[Any, A, C] {
def apply(from: Any): Builder[A, C] = apply()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package scala.collection.compat

import scala.collection.generic._
import scala.reflect.ClassTag
import scala.collection.{MapLike, GenTraversable, BitSet, IterableView}
import scala.collection.{LinearSeq, MapLike, GenTraversable, BitSet, IterableView}
import scala.collection.{immutable => i, mutable => m}
import scala.{collection => c}

Expand Down Expand Up @@ -45,8 +45,12 @@ private[compat] trait PackageShared {
}

implicit def genericCompanionToCBF[A, CC[X] <: GenTraversable[X]](
fact: GenericCompanion[CC]): CanBuildFrom[Any, A, CC[A]] =
simpleCBF(fact.newBuilder[A])
fact: GenericCompanion[CC]): CanBuildFrom[Any, A, CC[A]] = {
val builder = if (fact == Seq) new IdentityPreservingSeqBuilder[A](Seq.newBuilder[A]).asInstanceOf[m.Builder[A, CC[A]]]
else if (fact == LinearSeq) new IdentityPreservingLinearSeqBuilder[A](LinearSeq.newBuilder[A]).asInstanceOf[m.Builder[A, CC[A]]]
else fact.newBuilder[A]
simpleCBF(builder)
}

implicit def sortedSetCompanionToCBF[A: Ordering,
CC[X] <: c.SortedSet[X] with c.SortedSetLike[X, CC[X]]](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.junit.Test

import scala.collection.compat._
import scala.collection.immutable.BitSet
import scala.collection.LinearSeq

class CollectionTest {
@Test
Expand All @@ -40,6 +41,11 @@ class CollectionTest {
//val mT: Map[Int, String] = m
assertEquals(Map(1 -> "a", 2 -> "b"), m)
assertTrue(m.isInstanceOf[Map[_, _]])

// Stream.to(Seq) doesn't evaluate the stream
val strm = 1 #:: {throw new Exception("not lazy")} #:: Stream.empty[Int]
val strmsq: Seq[Int] = strm.to(Seq)
var strmln: LinearSeq[Int] = strm.to(LinearSeq)
}

@Test
Expand Down