Usually you do not create views when map
ping them, since you want to defer the creation of a collection of results until you see force
.
Since Tuple2Zipped
not a view, it tries to build a result on it that is the same type as its first set, which is a view.
SeqView
CanBuildFrom
gives NoBuilder
, which refuses forcibly.
Since the point of using Tuple2Zipped
is to avoid intermediate collections, you should also avoid premature forcing, so you need to make a representation before displaying:
scala> Seq(1,2,3).view(1,3) res0: scala.collection.SeqView[Int,Seq[Int]] = SeqViewS(...) scala> Seq(1,2,3).view(0,2) res1: scala.collection.SeqView[Int,Seq[Int]] = SeqViewS(...) scala> (res0, res1).zipped res2: scala.runtime.Tuple2Zipped[Int,scala.collection.SeqView[Int,Seq[Int]],Int,scala.collection.SeqView[Int,Seq[Int]]] = (SeqViewS(...), SeqViewS(...)).zipped scala> res2.view map { case (i: Int, j: Int) => i - j } res3: scala.collection.TraversableView[Int,Traversable[_]] = TraversableViewM(...) scala> .force res4: Traversable[Int] = List(1, 1)
Here we consider the mechanism:
import collection.generic.CanBuildFrom import collection.SeqView import collection.mutable.ListBuffer import language._ object Test extends App { implicit val cbf = new CanBuildFrom[SeqView[Int, Seq[Int]], Int, Seq[Int]] { def apply(): scala.collection.mutable.Builder[Int,Seq[Int]] = ListBuffer.empty[Int] def apply(from: scala.collection.SeqView[Int,Seq[Int]]): scala.collection.mutable.Builder[Int,Seq[Int]] = apply() } //val res = (6 to 10 view, 1 to 5 view).zipped.map[Int, List[Int]](_ - _) val res = (6 to 10 view, 1 to 5 view).zipped.map(_ - _) Console println res }