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

Commit 83b2836

Browse files
Rename TupleNZipped to LazyZipN
1 parent c7d7600 commit 83b2836

File tree

2 files changed

+67
-68
lines changed

2 files changed

+67
-68
lines changed

collections/src/main/scala/strawman/collection/LazyZipOps.scala

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.language.implicitConversions
66
final class LazyZipOps[A, C1 <: Iterable[A]] private[collection](`this`: C1) {
77

88
/** Analogous to `zip` except that the elements in each collection are not consumed until a strict operation is
9-
* invoked on the returned `Tuple2Zipped` decorator.
9+
* invoked on the returned `LazyZip2` decorator.
1010
*
1111
* Calls to `lazyZip` can be chained to support higher arities (up to 4) without incurring the expense of
1212
* constructing and deconstructing intermediary tuples.
@@ -17,26 +17,26 @@ final class LazyZipOps[A, C1 <: Iterable[A]] private[collection](`this`: C1) {
1717
* // res == List(4, 8, 12)
1818
* }}}
1919
*
20-
* @param that the iterable providing the second half of each eventual pair
21-
* @tparam B the type of the element in the second half of each eventual pair
22-
* @return a decorator `Tuple2Zipped` that allows strict operations to be performed on the lazily evaluated pairs
20+
* @param that the iterable providing the second element of each eventual pair
21+
* @tparam B the type of the second element in each eventual pair
22+
* @return a decorator `LazyZip2` that allows strict operations to be performed on the lazily evaluated pairs
2323
* or chained calls to `lazyZip`. Implicit conversion to `Iterable[(A, B)]` is also supported.
2424
*/
25-
def lazyZip[B](that: Iterable[B]): Tuple2Zipped[A, B, C1] = new Tuple2Zipped(`this`, that)
25+
def lazyZip[B](that: Iterable[B]): LazyZip2[A, B, C1] = new LazyZip2(`this`, that)
2626
}
2727

28-
/** Decorator representing lazily zipped tuples of arity 2. */
29-
final class Tuple2Zipped[El1, El2, C1 <: Iterable[El1]] private[collection](coll1: C1, coll2: Iterable[El2]) {
28+
/** Decorator representing lazily zipped pairs. */
29+
final class LazyZip2[El1, El2, C1 <: Iterable[El1]] private[collection](coll1: C1, coll2: Iterable[El2]) {
3030

31-
/** Zips `that` iterable collection with an already lazily zipped `Tuple2Zipped`. The elements in each collection are
32-
* not consumed until a strict operation is invoked on the returned `Tuple3Zipped` decorator.
31+
/** Zips `that` iterable collection with an existing `LazyZip2`. The elements in each collection are
32+
* not consumed until a strict operation is invoked on the returned `LazyZip3` decorator.
3333
*
34-
* @param that the iterable providing the third element of each eventual tuple
35-
* @tparam B the type of the third element in each eventual tuple
36-
* @return a decorator `Tuple3Zipped` that allows strict operations to be performed on the lazily evaluated tuples or
34+
* @param that the iterable providing the third element of each eventual triple
35+
* @tparam B the type of the third element in each eventual triple
36+
* @return a decorator `LazyZip3` that allows strict operations to be performed on the lazily evaluated tuples or
3737
* chained calls to `lazyZip`. Implicit conversion to `Iterable[(El1, El2, B)]` is also supported.
3838
*/
39-
def lazyZip[B](that: Iterable[B]): Tuple3Zipped[El1, El2, B, C1] = new Tuple3Zipped(coll1, coll2, that)
39+
def lazyZip[B](that: Iterable[B]): LazyZip3[El1, El2, B, C1] = new LazyZip3(coll1, coll2, that)
4040

4141
def map[B, C](f: (El1, El2) => B)(implicit bf: BuildFrom[C1, B, C]): C = {
4242
bf.fromSpecificIterable(coll1)(new View[B] {
@@ -121,26 +121,26 @@ final class Tuple2Zipped[El1, El2, C1 <: Iterable[El1]] private[collection](coll
121121
override def toString = s"$coll1.lazyZip($coll2)"
122122
}
123123

124-
object Tuple2Zipped {
125-
implicit def tuple2ZippedToIterable[El1, El2](zipped2: Tuple2Zipped[El1, El2, _]): View[(El1, El2)] =
124+
object LazyZip2 {
125+
implicit def lazyZip2ToIterable[El1, El2](zipped2: LazyZip2[El1, El2, _]): View[(El1, El2)] =
126126
new View[(El1, El2)] { def iterator() = zipped2.iterator() }
127127
}
128128

129129

130-
/** Decorator representing lazily zipped tuples of arity 3. */
131-
final class Tuple3Zipped[El1, El2, El3, C1 <: Iterable[El1]] private[collection](coll1: C1,
132-
coll2: Iterable[El2],
133-
coll3: Iterable[El3]) {
130+
/** Decorator representing lazily zipped triples. */
131+
final class LazyZip3[El1, El2, El3, C1 <: Iterable[El1]] private[collection](coll1: C1,
132+
coll2: Iterable[El2],
133+
coll3: Iterable[El3]) {
134134

135-
/** Zips `that` iterable collection with an already lazily zipped `Tuple3Zipped`. The elements in each collection are
136-
* not consumed until a strict operation is invoked on the returned `Tuple4Zipped` decorator.
135+
/** Zips `that` iterable collection with an existing `LazyZip3`. The elements in each collection are
136+
* not consumed until a strict operation is invoked on the returned `LazyZip4` decorator.
137137
*
138-
* @param that the iterable providing the fourth element of each eventual tuple
139-
* @tparam B the type of the fourth element in each eventual tuple
140-
* @return a decorator `Tuple4Zipped` that allows strict operations to be performed on the lazily evaluated tuples.
138+
* @param that the iterable providing the fourth element of each eventual 4-tuple
139+
* @tparam B the type of the fourth element in each eventual 4-tuple
140+
* @return a decorator `LazyZip4` that allows strict operations to be performed on the lazily evaluated tuples.
141141
* Implicit conversion to `Iterable[(El1, El2, El3, B)]` is also supported.
142142
*/
143-
def lazyZip[B](that: Iterable[B]): Tuple4Zipped[El1, El2, El3, B, C1] = new Tuple4Zipped(coll1, coll2, coll3, that)
143+
def lazyZip[B](that: Iterable[B]): LazyZip4[El1, El2, El3, B, C1] = new LazyZip4(coll1, coll2, coll3, that)
144144

145145
def map[B, C](f: (El1, El2, El3) => B)(implicit bf: BuildFrom[C1, B, C]): C = {
146146
bf.fromSpecificIterable(coll1)(new View[B] {
@@ -234,18 +234,18 @@ final class Tuple3Zipped[El1, El2, El3, C1 <: Iterable[El1]] private[collection]
234234
override def toString = s"$coll1.lazyZip($coll2).lazyZip($coll3)"
235235
}
236236

237-
object Tuple3Zipped {
238-
implicit def tuple3ZippedToIterable[El1, El2, El3](zipped3: Tuple3Zipped[El1, El2, El3, _]): View[(El1, El2, El3)] =
237+
object LazyZip3 {
238+
implicit def lazyZip3ToIterable[El1, El2, El3](zipped3: LazyZip3[El1, El2, El3, _]): View[(El1, El2, El3)] =
239239
new View[(El1, El2, El3)] { def iterator() = zipped3.iterator() }
240240
}
241241

242242

243243

244-
/** Decorator representing lazily zipped tuples of arity 4. */
245-
final class Tuple4Zipped[El1, El2, El3, El4, C1 <: Iterable[El1]] private[collection](coll1: C1,
246-
coll2: Iterable[El2],
247-
coll3: Iterable[El3],
248-
coll4: Iterable[El4]) {
244+
/** Decorator representing lazily zipped 4-tuples. */
245+
final class LazyZip4[El1, El2, El3, El4, C1 <: Iterable[El1]] private[collection](coll1: C1,
246+
coll2: Iterable[El2],
247+
coll3: Iterable[El3],
248+
coll4: Iterable[El4]) {
249249

250250
def map[B, C](f: (El1, El2, El3, El4) => B)(implicit bf: BuildFrom[C1, B, C]): C = {
251251
bf.fromSpecificIterable(coll1)(new View[B] {
@@ -346,8 +346,7 @@ final class Tuple4Zipped[El1, El2, El3, El4, C1 <: Iterable[El1]] private[collec
346346
override def toString = s"$coll1.lazyZip($coll2).lazyZip($coll3).lazyZip($coll4)"
347347
}
348348

349-
object Tuple4Zipped {
350-
implicit def tuple4ZippedToIterable[El1, El2, El3, El4](zipped4: Tuple4Zipped[El1, El2, El3, El4, _]
351-
): View[(El1, El2, El3, El4)] =
349+
object LazyZip4 {
350+
implicit def lazyZip4ToIterable[El1, El2, El3, El4](zipped4: LazyZip4[El1, El2, El3, El4, _]): View[(El1, El2, El3, El4)] =
352351
new View[(El1, El2, El3, El4)] { def iterator() = zipped4.iterator() }
353352
}

test/junit/src/test/scala/strawman/collection/LazyZipOpsTest.scala

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,68 +22,68 @@ class LazyZipOpsTest {
2222
private val sortedSet = TreeSet(1, 2, 3)
2323

2424
@Test
25-
def tuple2Zipped_map(): Unit = {
25+
def lazyZip2_map(): Unit = {
2626
val res: List[(Int, Int)] = zipped2.map((a, b) => (a, b))
2727

2828
assertEquals(List((1, 1), (2, 2), (3, 3)), res)
2929
}
3030

3131
@Test
32-
def tuple2Zipped_flatMap(): Unit = {
32+
def lazyZip2_flatMap(): Unit = {
3333
val res: List[(Int, Int)] = zipped2.flatMap((a, b) => List((a, b)))
3434

3535
assertEquals(List((1, 1), (2, 2), (3, 3)), res)
3636
}
3737

3838
@Test
39-
def tuple2Zipped_filter(): Unit = {
39+
def lazyZip2_filter(): Unit = {
4040
val res: List[(Int, Int)] = zipped2.filter((a, _) => a % 2 == 0)
4141

4242
assertEquals(List((2, 2)), res)
4343
}
4444

4545
@Test
46-
def tuple2Zipped_exists(): Unit = {
46+
def lazyZip2_exists(): Unit = {
4747
assertTrue(zipped2.exists((a, b) => a + b > 5))
4848
assertFalse(zipped2.exists((a, b) => a + b < 0))
4949
}
5050

5151
@Test
52-
def tuple2Zipped_forall(): Unit = {
52+
def lazyZip2_forall(): Unit = {
5353
assertTrue(zipped2.forall((a, b) => a + b > 0))
5454
assertFalse(zipped2.forall((a, b) => a + b > 2))
5555
}
5656

5757
@Test
58-
def tuple2Zipped_foreach(): Unit = {
58+
def lazyZip2_foreach(): Unit = {
5959
var res = ""
6060
zipped2.foreach((a, b) => res += s"[$a,$b]")
6161

6262
assertEquals("[1,1][2,2][3,3]", res)
6363
}
6464

6565
@Test
66-
def tuple2Zipped_toIterable(): Unit = {
66+
def lazyZip2_toIterable(): Unit = {
6767
val iter: Iterable[(Int, Int)] = zipped2
6868

6969
assertEquals(List((1, 1), (2, 2), (3, 3)), iter.to(List))
7070
}
7171

7272
@Test
73-
def tuple2Zipped_empty(): Unit = {
73+
def lazyZip2_empty(): Unit = {
7474
assertTrue(Nil.lazyZip(xs).isEmpty)
7575
assertTrue(xs.lazyZip(Nil).isEmpty)
7676
}
7777

7878
@Test
79-
def tuple2Zipped_withOrdering(): Unit = {
79+
def lazyZip2_withOrdering(): Unit = {
8080
val res: TreeSet[Int] = sortedSet.lazyZip(ws).map(_ + _)
8181

8282
assertEquals(TreeSet(2, 4, 6), res)
8383
}
8484

8585
@Test
86-
def tuple2Zipped_withMap(): Unit = {
86+
def lazyZip2_withMap(): Unit = {
8787
val res: Map[Int, (String, String)] = map.lazyZip(ys).map { case ((k, v), s) => k -> (s, v) }
8888

8989
assertThat(res, either(
@@ -93,75 +93,75 @@ class LazyZipOpsTest {
9393
}
9494

9595
@Test
96-
def tuple2Zipped_withSortedMap(): Unit = {
96+
def lazyZip2_withSortedMap(): Unit = {
9797
val res: TreeMap[Int, (String, String)] = sortedMap.lazyZip(ys).map { case ((k, v), s) => k -> (s, v) }
9898

9999
assertEquals(Map(1 -> ("a", "foo"), 2 -> ("b", "bar")), res)
100100
}
101101

102102
@Test
103-
def tuple3Zipped_map(): Unit = {
103+
def lazyZip3_map(): Unit = {
104104
val res: List[(Int, Int, String)] = zipped3.map((a, b, c) => (a, b, c))
105105

106106
assertEquals(List((1, 1, "a"), (2, 2, "b"), (3, 3, "c")), res)
107107
}
108108

109109
@Test
110-
def tuple3Zipped_flatMap(): Unit = {
110+
def lazyZip3_flatMap(): Unit = {
111111
val res: List[(Int, Int, String)] = zipped3.flatMap((a, b, c) => List((a, b, c)))
112112

113113
assertEquals(List((1, 1, "a"), (2, 2, "b"), (3, 3, "c")), res)
114114
}
115115

116116
@Test
117-
def tuple3Zipped_filter(): Unit = {
117+
def lazyZip3_filter(): Unit = {
118118
val res: List[(Int, Int, String)] = zipped3.filter((a, _, _) => a % 2 != 0)
119119

120120
assertEquals(List((1, 1, "a"), (3, 3, "c")), res)
121121
}
122122

123123
@Test
124-
def tuple3Zipped_exists(): Unit = {
124+
def lazyZip3_exists(): Unit = {
125125
assertTrue(zipped3.exists((a, b, _) => a + b > 5))
126126
assertFalse(zipped3.exists((a, b, _) => a + b < 0))
127127
}
128128

129129
@Test
130-
def tuple3Zipped_forall(): Unit = {
130+
def lazyZip3_forall(): Unit = {
131131
assertTrue(zipped3.forall((a, b, _) => (a + b) % 2 == 0))
132132
assertFalse(zipped3.forall((a, b, _) => a + b > 5))
133133
}
134134

135135
@Test
136-
def tuple3Zipped_foreach(): Unit = {
136+
def lazyZip3_foreach(): Unit = {
137137
var res = ""
138138
zipped3.foreach((a, b, c) => res += s"[$a,$b,$c]")
139139

140140
assertEquals("[1,1,a][2,2,b][3,3,c]", res)
141141
}
142142

143143
@Test
144-
def tuple3Zipped_toIterable(): Unit = {
144+
def lazyZip3_toIterable(): Unit = {
145145
val iter: Iterable[(Int, Int, String)] = zipped3
146146

147147
assertEquals(List((1, 1, "a"), (2, 2, "b"), (3, 3, "c")), iter.to(List))
148148
}
149149

150150
@Test
151-
def tuple3Zipped_empty(): Unit = {
151+
def lazyZip3_empty(): Unit = {
152152
assertTrue(zipped2.lazyZip(Nil).isEmpty)
153153
assertTrue(Nil.lazyZip(Nil).lazyZip(xs).isEmpty)
154154
}
155155

156156
@Test
157-
def tuple3Zipped_withOrdering(): Unit = {
157+
def lazyZip3_withOrdering(): Unit = {
158158
val res: TreeSet[Int] = sortedSet.lazyZip(xs).lazyZip(ws).map(_ + _ + _)
159159

160160
assertEquals(TreeSet(3, 6, 9), res)
161161
}
162162

163163
@Test
164-
def tuple3Zipped_withMap(): Unit = {
164+
def lazyZip3_withMap(): Unit = {
165165
val res: Map[Int, (Int, String, String)] = map.lazyZip(ws).lazyZip(ys).map { case ((k, v), w, y) => k -> (w, y, v) }
166166

167167
assertThat(res, either(
@@ -171,76 +171,76 @@ class LazyZipOpsTest {
171171
}
172172

173173
@Test
174-
def tuple3Zipped_withSortedMap(): Unit = {
174+
def lazyZip3_withSortedMap(): Unit = {
175175
val res: TreeMap[Int, (Int, String, String)] = sortedMap.lazyZip(ws).lazyZip(ys)
176176
.map { case ((k, v), w, y) => k -> (w, y, v) }
177177

178178
assertEquals(Map(1 -> (1, "a", "foo"), 2 -> (2, "b", "bar")), res)
179179
}
180180

181181
@Test
182-
def tuple4Zipped_map(): Unit = {
182+
def lazyZip4_map(): Unit = {
183183
val res: List[(Int, Int, String, Boolean)] = zipped4.map((a, b, c, d) => (a, b, c, d))
184184

185185
assertEquals(List((1, 1, "a", true), (2, 2, "b", false), (3, 3, "c", true)), res)
186186
}
187187

188188
@Test
189-
def tuple4Zipped_flatMap(): Unit = {
189+
def lazyZip4_flatMap(): Unit = {
190190
val res: List[(Int, Int, String, Boolean)] = zipped4.flatMap((a, b, c, d) => List((a, b, c, d)))
191191

192192
assertEquals(List((1, 1, "a", true), (2, 2, "b", false), (3, 3, "c", true)), res)
193193
}
194194

195195
@Test
196-
def tuple4Zipped_filter(): Unit = {
196+
def lazyZip4_filter(): Unit = {
197197
val res: List[(Int, Int, String, Boolean)] = zipped4.filter((_, _, _, d) => d)
198198

199199
assertEquals(List((1, 1, "a", true), (3, 3, "c", true)), res)
200200
}
201201

202202
@Test
203-
def tuple4Zipped_exists(): Unit = {
203+
def lazyZip4_exists(): Unit = {
204204
assertTrue(zipped4.exists((a, b, c, d) => a + b > 5 && !c.isEmpty && d))
205205
assertFalse(zipped4.exists((a, b, c, d) => a + b > 5 && !c.isEmpty && !d))
206206
}
207207

208208
@Test
209-
def tuple4Zipped_forall(): Unit = {
209+
def lazyZip4_forall(): Unit = {
210210
assertTrue(zipped4.forall((a, b, _, _) => (a + b) % 2 == 0))
211211
assertFalse(zipped4.forall((a, b, _, d) => a + b > 0 && d))
212212
}
213213

214214
@Test
215-
def tuple4Zipped_foreach(): Unit = {
215+
def lazyZip4_foreach(): Unit = {
216216
var res = ""
217217
zipped4.foreach((a, b, c, d) => res += s"[$a,$b,$c,$d]")
218218

219219
assertEquals("[1,1,a,true][2,2,b,false][3,3,c,true]", res)
220220
}
221221

222222
@Test
223-
def tuple4Zipped_toIterable(): Unit = {
223+
def lazyZip4_toIterable(): Unit = {
224224
val iter: Iterable[(Int, Int, String, Boolean)] = zipped4
225225

226226
assertEquals(List((1, 1, "a", true), (2, 2, "b", false), (3, 3, "c", true)), iter.to(List))
227227
}
228228

229229
@Test
230-
def tuple4Zipped_empty(): Unit = {
230+
def lazyZip4_empty(): Unit = {
231231
assertTrue(zipped3.lazyZip(Nil).isEmpty)
232232
assertTrue(Nil.lazyZip(Nil).lazyZip(Nil).lazyZip(xs).isEmpty)
233233
}
234234

235235
@Test
236-
def tuple4Zipped_withOrdering(): Unit = {
236+
def lazyZip4_withOrdering(): Unit = {
237237
val res: TreeSet[Int] = sortedSet.lazyZip(xs).lazyZip(ws).lazyZip(ws).map(_ + _ + _ + _)
238238

239239
assertEquals(TreeSet(4, 8, 12), res)
240240
}
241241

242242
@Test
243-
def tuple4Zipped_withMap(): Unit = {
243+
def lazyZip4_withMap(): Unit = {
244244
val res: Map[Int, (Int, Int, String, String)] = map.lazyZip(ws).lazyZip(xs).lazyZip(ys)
245245
.map { case ((k, v), w, x, y) => k -> (w, x, y, v) }
246246

@@ -251,7 +251,7 @@ class LazyZipOpsTest {
251251
}
252252

253253
@Test
254-
def tuple4Zipped_withSortedMap(): Unit = {
254+
def lazyZip4_withSortedMap(): Unit = {
255255
val res: TreeMap[Int, (Int, Int, String, String)] = sortedMap.lazyZip(ws).lazyZip(xs).lazyZip(ys)
256256
.map { case ((k, v), w, x, y) => k -> (w, x, y, v) }
257257

0 commit comments

Comments
 (0)