Skip to content

move conversion for lazyZip into implicit conversion method #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions compat/src/main/scala-2.11/scala/collection/compat/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ package object compat extends compat.PackageShared {
// `CanBuildFrom` parameters are used as type constraints, they are not used
// at run-time, hence the dummy builder implementations
def apply(from: IterableView[(K, V), CC[K, V]]) = new TraversableView.NoBuilder
def apply() = new TraversableView.NoBuilder
def apply() = new TraversableView.NoBuilder
}

implicit def toTraversableLikeExtensionMethods[Repr](self: Repr)(
Expand All @@ -43,14 +43,16 @@ package object compat extends compat.PackageShared {
implicit def toSeqExtensionMethods[A](self: c.Seq[A]): SeqExtensionMethods[A] =
new SeqExtensionMethods[A](self)

implicit def toTrulyTraversableLikeExtensionMethods[T1, El1, Repr1](self: T1)(
implicit w1: T1 => TraversableLike[El1, Repr1])
: TrulyTraversableLikeExtensionMethods[T1, El1, Repr1] =
new TrulyTraversableLikeExtensionMethods[T1, El1, Repr1](self)
implicit def toTrulyTraversableLikeExtensionMethods[T1, El1, Repr1](self: T1)(
implicit w1: T1 => TraversableLike[El1, Repr1]
): TrulyTraversableLikeExtensionMethods[El1, Repr1] =
new TrulyTraversableLikeExtensionMethods[El1, Repr1](w1(self))

implicit def toTuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](
self: Tuple2Zipped[El1, Repr1, El2, Repr2]
): Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2] =
new Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](self)

implicit def toTuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](self: Tuple2Zipped[El1, Repr1, El2, Repr2])
: Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2] =
new Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](self)
implicit def toImmutableQueueExtensionMethods[A](
self: i.Queue[A]): ImmutableQueueExtensionMethods[A] =
new ImmutableQueueExtensionMethods[A](self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,20 +371,21 @@ class TraversableLikeExtensionMethods[A, Repr](private val self: c.GenTraversabl
}
}

class TrulyTraversableLikeExtensionMethods[T1, El1, Repr1](private val self: T1) extends AnyVal {
def lazyZip[El2, Repr2, T2](t2: T2)(
implicit w: T1 => TraversableLike[El1, Repr1],
w2: T2 => IterableLike[El2, Repr2]): Tuple2Zipped[El1, Repr1, El2, Repr2] =
new Tuple2Zipped((w(self), t2))
class TrulyTraversableLikeExtensionMethods[El1, Repr1](
private val self: TraversableLike[El1, Repr1])
extends AnyVal {

def lazyZip[El2, Repr2, T2](t2: T2)(
implicit w2: T2 => IterableLike[El2, Repr2]
): Tuple2Zipped[El1, Repr1, El2, Repr2] = new Tuple2Zipped((self, t2))
}

class Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](private val self: Tuple2Zipped[El1, Repr1, El2, Repr2]) {
def lazyZip[El3, Repr3, T3](t3: T3)(implicit w3: T3 => IterableLike[El3, Repr3])
: Tuple3Zipped[El1, Repr1, El2, Repr2, El3, Repr3] = {
class Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](
private val self: Tuple2Zipped[El1, Repr1, El2, Repr2]) {

new Tuple3Zipped((self.colls._1, self.colls._2, t3))
}
def lazyZip[El3, Repr3, T3](t3: T3)(implicit w3: T3 => IterableLike[El3, Repr3])
: Tuple3Zipped[El1, Repr1, El2, Repr2, El3, Repr3] =
new Tuple3Zipped((self.colls._1, self.colls._2, t3))
}

class MapViewExtensionMethods[K, V, C <: scala.collection.Map[K, V]](
Expand Down
22 changes: 11 additions & 11 deletions compat/src/main/scala-2.12/scala/collection/compat/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import scala.collection.{mutable => m}
import scala.runtime.Tuple2Zipped
import scala.collection.{immutable => i, mutable => m}


package object compat extends compat.PackageShared {
implicit class MutableTreeMapExtensions2(private val fact: m.TreeMap.type) extends AnyVal {
def from[K: Ordering, V](source: TraversableOnce[(K, V)]): m.TreeMap[K, V] =
Expand All @@ -38,12 +37,13 @@ package object compat extends compat.PackageShared {
// CanBuildFrom instances for `IterableView[(K, V), Map[K, V]]` that preserve
// the strict type of the view to be `Map` instead of `Iterable`
// Instances produced by this method are used to chain `filterKeys` after `mapValues`
implicit def canBuildFromIterableViewMapLike[K, V, L, W, CC[X, Y] <: Map[X, Y]]: CanBuildFrom[IterableView[(K, V), CC[K, V]], (L, W), IterableView[(L, W), CC[L, W]]] =
implicit def canBuildFromIterableViewMapLike[K, V, L, W, CC[X, Y] <: Map[X, Y]]
: CanBuildFrom[IterableView[(K, V), CC[K, V]], (L, W), IterableView[(L, W), CC[L, W]]] =
new CanBuildFrom[IterableView[(K, V), CC[K, V]], (L, W), IterableView[(L, W), CC[L, W]]] {
// `CanBuildFrom` parameters are used as type constraints, they are not used
// at run-time, hence the dummy builder implementations
def apply(from: IterableView[(K, V), CC[K, V]]) = new TraversableView.NoBuilder
def apply() = new TraversableView.NoBuilder
def apply() = new TraversableView.NoBuilder
}

implicit def toTraversableLikeExtensionMethods[Repr](self: Repr)(
Expand All @@ -54,17 +54,17 @@ package object compat extends compat.PackageShared {
implicit def toSeqExtensionMethods[A](self: c.Seq[A]): SeqExtensionMethods[A] =
new SeqExtensionMethods[A](self)

implicit def toTrulyTraversableLikeExtensionMethods[T1, El1, Repr1](self: T1)(
implicit w1: T1 => TraversableLike[El1, Repr1])
: TrulyTraversableLikeExtensionMethods[T1, El1, Repr1] =
new TrulyTraversableLikeExtensionMethods[T1, El1, Repr1](self)
implicit def toTrulyTraversableLikeExtensionMethods[T1, El1, Repr1](self: T1)(
implicit w1: T1 => TraversableLike[El1, Repr1]
): TrulyTraversableLikeExtensionMethods[El1, Repr1] =
new TrulyTraversableLikeExtensionMethods[El1, Repr1](w1(self))

implicit def toTuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](self: Tuple2Zipped[El1, Repr1, El2, Repr2])
: Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2] =
new Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](self)
implicit def toTuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](
self: Tuple2Zipped[El1, Repr1, El2, Repr2])
: Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2] =
new Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](self)

implicit def toImmutableQueueExtensionMethods[A](
self: i.Queue[A]): ImmutableQueueExtensionMethods[A] =
new ImmutableQueueExtensionMethods[A](self)
}

64 changes: 32 additions & 32 deletions compat/src/test/scala/test/scala/collection/LazyZipTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ import scala.collection.compat._

class LazyZipTest {

private val ws = List(1, 2, 3)
private val xs = List(1, 2, 3, 4, 5, 6)
private val ys = List("a", "b", "c", "d", "e", "f")
private val zs = List(true, false, true, false, true, false)
private val zipped2 = ws lazyZip xs
private val zipped3 = ws lazyZip xs lazyZip ys
private val zipped4 = ws lazyZip xs lazyZip ys lazyZip zs
private val map = Map(1 -> "foo" , 2 -> "bar")

@Test
def lazyZipTest(): Unit = {
val res: List[(Int, Int)] = zipped2.map((a, b) => (a, b))
assertEquals(List((1, 1), (2, 2), (3, 3)), res)
}

@Test
def lazyZip3_map(): Unit = {
val res: List[(Int, Int, String)] = zipped3.map((a: Int, b: Int, c: String) => (a, b, c))
assertEquals(List((1, 1, "a"), (2, 2, "b"), (3, 3, "c")), res)
}

@Test
def collectionValueIsNotEvaluated(): Unit = {
val st = Stream.cons(1, throw new AssertionError("should not be evaluated"))
ws.lazyZip(st)
}

@Test
def zip3collectionValueIsNotEvaluated(): Unit = {
val st = Stream.cons(1, throw new AssertionError("should not be evaluated"))
ws.lazyZip(st).lazyZip(st)
}
private val ws = List(1, 2, 3)
private val xs = List(1, 2, 3, 4, 5, 6)
private val ys = List("a", "b", "c", "d", "e", "f")
private val zs = List(true, false, true, false, true, false)
private val zipped2 = ws lazyZip xs
private val zipped3 = ws lazyZip xs lazyZip ys
private val zipped4 = ws lazyZip xs lazyZip ys lazyZip zs
private val map = Map(1 -> "foo", 2 -> "bar")

@Test
def lazyZipTest(): Unit = {
val res: List[(Int, Int)] = zipped2.map((a, b) => (a, b))
assertEquals(List((1, 1), (2, 2), (3, 3)), res)
}

@Test
def lazyZip3_map(): Unit = {
val res: List[(Int, Int, String)] = zipped3.map((a: Int, b: Int, c: String) => (a, b, c))
assertEquals(List((1, 1, "a"), (2, 2, "b"), (3, 3, "c")), res)
}

@Test
def collectionValueIsNotEvaluated(): Unit = {
val st = Stream.cons(1, throw new AssertionError("should not be evaluated"))
ws.lazyZip(st)
}

@Test
def zip3collectionValueIsNotEvaluated(): Unit = {
val st = Stream.cons(1, throw new AssertionError("should not be evaluated"))
ws.lazyZip(st).lazyZip(st)
}

}