summaryrefslogtreecommitdiff
path: root/src/library/scala/Tuple2.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2012-04-27 07:24:46 -0700
committerPaul Phillips <paulp@improving.org>2012-04-27 07:24:46 -0700
commit1c3f66b6f2d41c02cc2bc32cb696aafa56b71176 (patch)
treeafd2ec64dc39ac29c2fc15221e431d2d8548a9e7 /src/library/scala/Tuple2.scala
parent57574bb10833e90338bd0eadef9dabe260b8afda (diff)
downloadscala-1c3f66b6f2d41c02cc2bc32cb696aafa56b71176.tar.gz
scala-1c3f66b6f2d41c02cc2bc32cb696aafa56b71176.tar.bz2
scala-1c3f66b6f2d41c02cc2bc32cb696aafa56b71176.zip
Revert "Moved ancillary methods off specialized traits."
This reverts commit 1d0372f84f9a7325a47beb55169cc454895ef74b. I forgot about polymorphic dispatch. Have to seek another way.
Diffstat (limited to 'src/library/scala/Tuple2.scala')
-rw-r--r--src/library/scala/Tuple2.scala114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/library/scala/Tuple2.scala b/src/library/scala/Tuple2.scala
index 924b2b6fe3..37ab564c3c 100644
--- a/src/library/scala/Tuple2.scala
+++ b/src/library/scala/Tuple2.scala
@@ -9,6 +9,9 @@
package scala
+import scala.collection.{ TraversableLike => TLike, IterableLike => ILike }
+import scala.collection.generic.{ CanBuildFrom => CBF }
+
/** A tuple of 2 elements; the canonical representation of a [[scala.Product2]].
*
@@ -27,4 +30,115 @@ case class Tuple2[@specialized(Int, Long, Double, Char, Boolean, AnyRef) +T1, @s
*/
def swap: Tuple2[T2,T1] = Tuple2(_2, _1)
+ @deprecated("Use `zipped` instead.", "2.9.0")
+ def zip[Repr1, El1, El2, To](implicit w1: T1 => TLike[El1, Repr1],
+ w2: T2 => Iterable[El2],
+ cbf1: CBF[Repr1, (El1, El2), To]): To = {
+ zipped map ((x, y) => ((x, y)))
+ }
+
+ /** Wraps a tuple in a `Zipped`, which supports 2-ary generalisations of `map`, `flatMap`, `filter`, etc.
+ * Note that there must be an implicit value to convert this tuple's types into a [[scala.collection.TraversableLike]]
+ * or [[scala.collection.IterableLike]].
+ * {{{
+ * scala> val tuple = (List(1,2,3),List('a','b','c'))
+ * tuple: (List[Int], List[Char]) = (List(1, 2, 3),List(a, b, c))
+ *
+ * scala> tuple.zipped map { (x,y) => x + ":" + y }
+ * res6: List[java.lang.String] = List(1:a, 2:b, 3:c)
+ * }}}
+ *
+ * @see Zipped
+ * Note: will not terminate for infinite-sized collections.
+ */
+ def zipped[Repr1, El1, Repr2, El2](implicit w1: T1 => TLike[El1, Repr1], w2: T2 => ILike[El2, Repr2]): Zipped[Repr1, El1, Repr2, El2]
+ = new Zipped[Repr1, El1, Repr2, El2](_1, _2)
+
+ /**
+ * @define coll zipped
+ * @define Coll Zipped
+ * @define orderDependent
+ * @define orderDependentFold
+ * @define mayNotTerminateInf
+ * @define willNotTerminateInf
+ * @define collectExample
+ * @define undefinedorder
+ */
+ class Zipped[+Repr1, +El1, +Repr2, +El2](coll1: TLike[El1, Repr1], coll2: ILike[El2, Repr2]) { // coll2: ILike for filter
+ def map[B, To](f: (El1, El2) => B)(implicit cbf: CBF[Repr1, B, To]): To = {
+ val b = cbf(coll1.repr)
+ b.sizeHint(coll1)
+ val elems2 = coll2.iterator
+
+ for (el1 <- coll1) {
+ if (elems2.hasNext)
+ b += f(el1, elems2.next)
+ else
+ return b.result
+ }
+
+ b.result
+ }
+
+ def flatMap[B, To](f: (El1, El2) => TraversableOnce[B])(implicit cbf: CBF[Repr1, B, To]): To = {
+ val b = cbf(coll1.repr)
+ val elems2 = coll2.iterator
+
+ for (el1 <- coll1) {
+ if (elems2.hasNext)
+ b ++= f(el1, elems2.next)
+ else
+ return b.result
+ }
+
+ b.result
+ }
+
+ def filter[To1, To2](f: (El1, El2) => Boolean)(implicit cbf1: CBF[Repr1, El1, To1], cbf2: CBF[Repr2, El2, To2]): (To1, To2) = {
+ val b1 = cbf1(coll1.repr)
+ val b2 = cbf2(coll2.repr)
+ val elems2 = coll2.iterator
+
+ for (el1 <- coll1) {
+ if (elems2.hasNext) {
+ val el2 = elems2.next
+ if (f(el1, el2)) {
+ b1 += el1
+ b2 += el2
+ }
+ }
+ else return (b1.result, b2.result)
+ }
+
+ (b1.result, b2.result)
+ }
+
+ def exists(f: (El1, El2) => Boolean): Boolean = {
+ val elems2 = coll2.iterator
+
+ for (el1 <- coll1) {
+ if (elems2.hasNext) {
+ if (f(el1, elems2.next))
+ return true
+ }
+ else return false
+ }
+ false
+ }
+
+ def forall(f: (El1, El2) => Boolean): Boolean =
+ !exists((x, y) => !f(x, y))
+
+ def foreach[U](f: (El1, El2) => U): Unit = {
+ val elems2 = coll2.iterator
+
+ for (el1 <- coll1) {
+ if (elems2.hasNext)
+ f(el1, elems2.next)
+ else
+ return
+ }
+ }
+ }
+
}