Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Commit

Permalink
Implement zip in terms of zipWith
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelocenerine authored and root committed Sep 4, 2017
1 parent 5d4e65f commit 9ee2026
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/strawman/collection/ArrayOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/strawman/collection/Iterable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/main/scala/strawman/collection/Iterator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/strawman/collection/SortedSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 0 additions & 8 deletions src/main/scala/strawman/collection/View.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions src/main/scala/strawman/collection/immutable/LazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9ee2026

Please sign in to comment.