Skip to content
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

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

Merged
merged 9 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all 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,14 +12,58 @@

package scala.collection.compat

import scala.reflect.ClassTag
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 IdentityPreservingBuilder[A, CC[X] <: TraversableOnce[X]](that: m.Builder[A, CC[A]])(implicit ct: ClassTag[CC[A]])
extends m.Builder[A, CC[A]] {

//invariant: ruined => (collection == null)
var collection: CC[A] = null.asInstanceOf[CC[A]]
var ruined = false

private[this] def ruin(): Unit = {
if(collection != null) that ++= collection
collection = null.asInstanceOf[CC[A]]
NthPortal marked this conversation as resolved.
Show resolved Hide resolved
ruined = true
}

override def ++=(elems: TraversableOnce[A]): this.type =
elems match {
case ct(ca) if collection == null && !ruined => {
collection = ca
this
}
case _ => {
ruin()
that ++= elems
this
}
}

def +=(elem: A): this.type = {
ruin()
that += elem
this
}

def clear(): Unit = {
collection = null.asInstanceOf[CC[A]]
if (ruined) that.clear()
NthPortal marked this conversation as resolved.
Show resolved Hide resolved
ruined = false
}

def result(): CC[A] = if(collection == null) that.result() 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()
def apply(): Builder[A, C] = f
def simpleCBF[A, C](f: => m.Builder[A, C]): CanBuildFrom[Any, A, C] = new CanBuildFrom[Any, A, C] {
def apply(from: Any): m.Builder[A, C] = apply()
def apply(): m.Builder[A, C] = f
}

type ImmutableBitSetCC[X] = ({ type L[_] = i.BitSet })#L[X]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@ 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: m.Builder[A, CC[A]] = fact match {
case c.Seq | i.Seq => new IdentityPreservingBuilder[A, i.Seq](i.Seq.newBuilder[A])
case c.LinearSeq | i.LinearSeq =>
new IdentityPreservingBuilder[A, i.LinearSeq](i.LinearSeq.newBuilder[A])
case _ => 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