diff options
author | Paul Phillips <paulp@improving.org> | 2010-10-11 23:17:47 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-10-11 23:17:47 +0000 |
commit | e5c22d9e0a11f0c1a0d29520f7420d709fb174ec (patch) | |
tree | 819aed10c296f998cc082acb24b4835cd2eca224 /test/files/run/tuple-zipped.scala | |
parent | 5c83be3b2bb200f53abd5e3c6667e78ce01ebe49 (diff) | |
download | scala-e5c22d9e0a11f0c1a0d29520f7420d709fb174ec.tar.gz scala-e5c22d9e0a11f0c1a0d29520f7420d709fb174ec.tar.bz2 scala-e5c22d9e0a11f0c1a0d29520f7420d709fb174ec.zip |
An overhaul of the collection-oriented methods ...
An overhaul of the collection-oriented methods in Tuple2/Tuple3 (which
still need to be taken all the way to Tuple5.)
* Zip semantics: zip and zipped now properly terminate when any
collection runs out of elements, even if others are infinite. In
addition, short circuiting methods (exists and forall) will terminate if
the condition is met, even in the presence of infinity.
Example:
scala> val ys = Stream from 1
ys: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> (ys, ys).zipped forall ((x, y) => x+y < 100)
res0: Boolean = false
scala> (ys, ys).zipped exists ((x, y) => x+y > 100)
res1: Boolean = true
* There are implicits converting Zipped2/3 to Traversable to expose all
the methods which aren't defined in an arity-specific way in the tuple
classes. I have mixed feelings about putting these in Predef; but if
there is another way to make them visible by default I wasn't able to
find it.
Example putting said implicit to use:
scala> (ys, ys, ys).zipped find { case (x, y, z) => x+y+z > 1000 }
res0: Option[(Int, Int, Int)] = Some((334,334,334))
Already reviewed by moors, so no review.
Diffstat (limited to 'test/files/run/tuple-zipped.scala')
-rw-r--r-- | test/files/run/tuple-zipped.scala | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/test/files/run/tuple-zipped.scala b/test/files/run/tuple-zipped.scala new file mode 100644 index 0000000000..8f482dc3bf --- /dev/null +++ b/test/files/run/tuple-zipped.scala @@ -0,0 +1,39 @@ +object Test { + val xs1 = List.range(1, 100) + val xs2 = xs1.view + val xs3 = xs1 take 10 + val ss1 = Stream from 1 + val ss2 = ss1.view + val ss3 = ss1 take 10 + val as1 = 1 to 100 toArray + val as2 = as1.view + val as3 = as1 take 10 + + def xss1 = List[Seq[Int]](xs1, xs2, xs3, ss1, ss2, ss3, as1, as2, as3) + def xss2 = List[Seq[Int]](xs1, xs2, xs3, ss3, as1, as2, as3) // no infinities + def xss3 = List[Seq[Int]](xs2, xs3, ss3, as1) // representative sampling + + def main(args: Array[String]): Unit = { + for (cc1 <- xss1 ; cc2 <- xss2) { + val sum1 = (cc1, cc2).zip map { case (x, y) => x + y } sum + val sum2 = (cc1, cc2).zipped map (_ + _) sum + + assert(sum1 == sum2) + } + + for (cc1 <- xss1 ; cc2 <- xss2 ; cc3 <- xss3) { + val sum1 = (cc1, cc2, cc3).zip map { case (x, y, z) => x + y + z } sum + val sum2 = (cc1, cc2, cc3).zipped map (_ + _ + _) sum + + assert(sum1 == sum2) + } + + assert((ss1, ss1).zipped exists ((x, y) => true)) + assert((ss1, ss1, ss1).zipped exists ((x, y, z) => true)) + + assert(!(ss1, ss2, 1 to 3).zipped.exists(_ + _ + _ > 100000)) + assert((1 to 3, ss1, ss2).zipped.forall(_ + _ + _ > 0)) + assert((ss1, 1 to 3, ss2).zipped.map(_ + _ + _).size == 3) + } +} + |