diff --git a/src/main/scala/strawman/collection/ArrayOps.scala b/src/main/scala/strawman/collection/ArrayOps.scala index 55d3d10f7e..721fe15dbe 100644 --- a/src/main/scala/strawman/collection/ArrayOps.scala +++ b/src/main/scala/strawman/collection/ArrayOps.scala @@ -52,7 +52,7 @@ class ArrayOps[A](val xs: Array[A]) def ++[B >: A : ClassTag](xs: Iterable[B]): Array[B] = fromTaggedIterable(View.Concat(toIterable, xs)) - def zip[B: ClassTag](xs: Iterable[B]): Array[(A, B)] = fromTaggedIterable(View.Zip(toIterable, xs)) + def zip[B: ClassTag](xs: Iterable[B]): Array[(A, B)] = zipWith(xs)((_, _)) def zipWith[B, R: ClassTag](xs: Iterable[B])(f: (A, B) => R): Array[R] = fromTaggedIterable(View.ZipWith(toIterable, xs, f)) } diff --git a/src/main/scala/strawman/collection/Iterable.scala b/src/main/scala/strawman/collection/Iterable.scala index 7a2bf60692..3236cebdb1 100644 --- a/src/main/scala/strawman/collection/Iterable.scala +++ b/src/main/scala/strawman/collection/Iterable.scala @@ -835,7 +835,8 @@ trait IterableOps[+A, +CC[X], +C] extends Any { * corresponding elements of this $coll and `that`. The length * of the returned collection is the minimum of the lengths of this $coll and `that`. */ - def zip[B](xs: Iterable[B]): CC[(A @uncheckedVariance, B)] = fromIterable(View.Zip(toIterable, xs)) + def zip[B](xs: Iterable[B]): CC[(A @uncheckedVariance, B)] = zipWith(xs)((_, _)) + // sound bcs of VarianceNote /** Returns a $coll formed by the result of applying a function to each pair of corresponding elements diff --git a/src/main/scala/strawman/collection/Iterator.scala b/src/main/scala/strawman/collection/Iterator.scala index 9169a13b6a..6dd25b30f7 100644 --- a/src/main/scala/strawman/collection/Iterator.scala +++ b/src/main/scala/strawman/collection/Iterator.scala @@ -607,11 +607,7 @@ trait Iterator[+A] extends IterableOnce[A] { self => else Iterator.empty.next() } - def zip[B](that: IterableOnce[B]): Iterator[(A, B)] = new Iterator[(A, B)] { - val thatIterator = that.iterator() - def hasNext = self.hasNext && thatIterator.hasNext - def next() = (self.next(), thatIterator.next()) - } + def zip[B](that: IterableOnce[B]): Iterator[(A, B)] = zipWith(that)((_, _)) def zipWith[B, R](that: IterableOnce[B])(f: (A, B) => R): Iterator[R] = new Iterator[R] { val thatIterator = that.iterator() diff --git a/src/main/scala/strawman/collection/SortedSet.scala b/src/main/scala/strawman/collection/SortedSet.scala index d8ec510a76..abaf0a7ada 100644 --- a/src/main/scala/strawman/collection/SortedSet.scala +++ b/src/main/scala/strawman/collection/SortedSet.scala @@ -39,7 +39,7 @@ trait SortedSetOps[A, +CC[X] <: SortedSet[X], +C <: SortedSetOps[A, CC, C]] /** Zip. Interesting because it requires to align to source collections. */ def zip[B](xs: Iterable[B])(implicit ev: Ordering[(A @uncheckedVariance, B)]): CC[(A @uncheckedVariance, B)] = // sound bcs of VarianceNote - sortedFromIterable(View.Zip(toIterable, xs)) + zipWith(xs)((_, _))(ev) def zipWith[B, R](xs: Iterable[B])(f: (A, B) => R)(implicit ev: Ordering[R]): CC[R] = sortedFromIterable(View.ZipWith(toIterable, xs, f)) diff --git a/src/main/scala/strawman/collection/View.scala b/src/main/scala/strawman/collection/View.scala index 98ed6ec25b..6270fcd7fe 100644 --- a/src/main/scala/strawman/collection/View.scala +++ b/src/main/scala/strawman/collection/View.scala @@ -183,14 +183,6 @@ object View extends IterableFactory[View] { else -1 } - /** A view that zips elements of the underlying collection with the elements - * of another collection or iterator. - */ - case class Zip[A, B](underlying: Iterable[A], other: Iterable[B]) extends View[(A, B)] { - def iterator() = underlying.iterator().zip(other) - override def knownSize = underlying.knownSize min other.knownSize - } - /** A view that generalizes the zip operation by applying a function to each pair of elements * in the underlying collection and another collection or iterator. */ diff --git a/src/main/scala/strawman/collection/immutable/ImmutableArray.scala b/src/main/scala/strawman/collection/immutable/ImmutableArray.scala index d258a52024..c5f44278ce 100644 --- a/src/main/scala/strawman/collection/immutable/ImmutableArray.scala +++ b/src/main/scala/strawman/collection/immutable/ImmutableArray.scala @@ -76,15 +76,7 @@ class ImmutableArray[+A] private[collection] (private val elements: scala.Array[ ImmutableArray.fromIterable(View.Concat(xs, toIterable)) } - override def zip[B](xs: collection.Iterable[B]): ImmutableArray[(A, B)] = - xs match { - case bs: ImmutableArray[B] => - ImmutableArray.tabulate(length min bs.length) { i => - (apply(i), bs(i)) - } - case _ => - ImmutableArray.fromIterable(View.Zip(toIterable, xs)) - } + override def zip[B](xs: collection.Iterable[B]): ImmutableArray[(A, B)] = zipWith(xs)((_, _)) override def zipWith[B, R](xs: collection.Iterable[B])(f: (A, B) => R): ImmutableArray[R] = xs match { diff --git a/src/main/scala/strawman/collection/immutable/LazyList.scala b/src/main/scala/strawman/collection/immutable/LazyList.scala index 17e2fd3b34..868107d5a5 100644 --- a/src/main/scala/strawman/collection/immutable/LazyList.scala +++ b/src/main/scala/strawman/collection/immutable/LazyList.scala @@ -172,9 +172,7 @@ sealed abstract class LazyList[+A] else prefix.lazyAppendAll(nonEmptyPrefix.tail.flatMap(f)) } - override final def zip[B](xs: collection.Iterable[B]): LazyList[(A, B)] = - if (this.isEmpty || xs.isEmpty) LazyList.empty - else LazyList.cons((this.head, xs.head), this.tail.zip(xs.tail)) + override final def zip[B](xs: collection.Iterable[B]): LazyList[(A, B)] = zipWith(xs)((_, _)) override final def zipWith[B, R](xs: collection.Iterable[B])(f: (A, B) => R): LazyList[R] = if (this.isEmpty || xs.isEmpty) LazyList.empty