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

Commit 01c982a

Browse files
authored
Merge pull request #223 from marcelocenerine/master
Add zipWith operation
2 parents dc90c73 + 7d1f06d commit 01c982a

24 files changed

+899
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ For more information, see the [CONTRIBUTING](CONTRIBUTING.md) file.
231231
- [x] `sliding`
232232
- [x] `unzip`
233233
- [x] `values` / `valuesIterator`
234-
- [x] `zip` / `zipWithIndex`
234+
- [x] `zip` / `zipWithIndex` / `lazyZip`
235235

236236
### In-place mutating operations
237237

benchmarks/time/src/main/scala/strawman/collection/immutable/HashSetBenchmark.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,21 @@ class HashSetBenchmark {
135135
}
136136
}
137137

138+
@Benchmark
139+
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))
140+
141+
@Benchmark
142+
def transform_zipMapTupled(bh: Blackhole): Unit = {
143+
val f = (a: Long, b: Long) => (a, b)
144+
bh.consume(xs.zip(xs).map(f.tupled))
145+
}
146+
147+
@Benchmark
148+
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)
149+
150+
@Benchmark
151+
def transform_lazyZip(bh: Blackhole): Unit = bh.consume(xs.lazyZip(xs).map((_, _)))
152+
138153
@Benchmark
139154
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)
140155

benchmarks/time/src/main/scala/strawman/collection/immutable/ImmutableArrayBenchmark.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,21 @@ class ImmutableArrayBenchmark {
273273
}
274274
}
275275

276+
@Benchmark
277+
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))
278+
279+
@Benchmark
280+
def transform_zipMapTupled(bh: Blackhole): Unit = {
281+
val f = (a: Long, b: Long) => (a, b)
282+
bh.consume(xs.zip(xs).map(f.tupled))
283+
}
284+
285+
@Benchmark
286+
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)
287+
288+
@Benchmark
289+
def transform_lazyZip(bh: Blackhole): Unit = bh.consume(xs.lazyZip(xs).map((_, _)))
290+
276291
@Benchmark
277292
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)
278293

benchmarks/time/src/main/scala/strawman/collection/immutable/LazyListBenchmark.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,21 @@ class LazyListBenchmark {
273273
}
274274
}
275275

276+
@Benchmark
277+
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))
278+
279+
@Benchmark
280+
def transform_zipMapTupled(bh: Blackhole): Unit = {
281+
val f = (a: Long, b: Long) => (a, b)
282+
bh.consume(xs.zip(xs).map(f.tupled))
283+
}
284+
285+
@Benchmark
286+
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)
287+
288+
@Benchmark
289+
def transform_lazyZip(bh: Blackhole): Unit = bh.consume(xs.lazyZip(xs).map((_, _)))
290+
276291
@Benchmark
277292
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)
278293

benchmarks/time/src/main/scala/strawman/collection/immutable/ListBenchmark.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,21 @@ class ListBenchmark {
273273
}
274274
}
275275

276+
@Benchmark
277+
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))
278+
279+
@Benchmark
280+
def transform_zipMapTupled(bh: Blackhole): Unit = {
281+
val f = (a: Long, b: Long) => (a, b)
282+
bh.consume(xs.zip(xs).map(f.tupled))
283+
}
284+
285+
@Benchmark
286+
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)
287+
288+
@Benchmark
289+
def transform_lazyZip(bh: Blackhole): Unit = bh.consume(xs.lazyZip(xs).map((_, _)))
290+
276291
@Benchmark
277292
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)
278293

benchmarks/time/src/main/scala/strawman/collection/immutable/NumericRangeBenchmark.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ class NumericRangeBenchmark {
2121
var xs: NumericRange[Int] = _
2222
var zs: NumericRange[Int] = _
2323
var randomIndices: scala.Array[Int] = _
24+
var zipped: IndexedSeq[(Long, Long)] = _
2425
def fresh(n: Int) = NumericRange.inclusive(1, n, 1)
2526

2627
@Setup(Level.Trial)
2728
def initTrial(): Unit = {
2829
xs = fresh(size)
2930
zs = NumericRange.inclusive(-1, (-size / 1000) min -2, -1)
31+
zipped = xs.map(x => (x, x))
3032
if (size > 0) {
3133
randomIndices = scala.Array.fill(1000)(scala.util.Random.nextInt(size))
3234
}
@@ -176,4 +178,25 @@ class NumericRangeBenchmark {
176178
val result = xs.groupBy(_ % 5)
177179
bh.consume(result)
178180
}
181+
182+
@Benchmark
183+
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))
184+
185+
@Benchmark
186+
def transform_zipMapTupled(bh: Blackhole): Unit = {
187+
val f = (a: Int, b: Int) => (a, b)
188+
bh.consume(xs.zip(xs).map(f.tupled))
189+
}
190+
191+
@Benchmark
192+
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)
193+
194+
@Benchmark
195+
def transform_lazyZip(bh: Blackhole): Unit = {
196+
val xs1: IndexedSeq[Int] = xs
197+
bh.consume(xs1.lazyZip(xs1).map((_, _)))
198+
}
199+
200+
@Benchmark
201+
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)
179202
}

benchmarks/time/src/main/scala/strawman/collection/immutable/RangeBenchmark.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ class RangeBenchmark {
2121
var xs: Range = _
2222
var zs: Range = _
2323
var randomIndices: scala.Array[Int] = _
24+
var zipped: IndexedSeq[(Long, Long)] = _
2425
def fresh(n: Int) = Range.inclusive(1, n, 1)
2526

2627
@Setup(Level.Trial)
2728
def initTrial(): Unit = {
2829
xs = fresh(size)
2930
zs = Range.inclusive(-1, (-size / 1000) min -2, -1)
31+
zipped = xs.map(x => (x, x))
3032
if (size > 0) {
3133
randomIndices = scala.Array.fill(1000)(scala.util.Random.nextInt(size))
3234
}
@@ -176,4 +178,25 @@ class RangeBenchmark {
176178
val result = xs.groupBy(_ % 5)
177179
bh.consume(result)
178180
}
181+
182+
@Benchmark
183+
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))
184+
185+
@Benchmark
186+
def transform_zipMapTupled(bh: Blackhole): Unit = {
187+
val f = (a: Int, b: Int) => (a, b)
188+
bh.consume(xs.zip(xs).map(f.tupled))
189+
}
190+
191+
@Benchmark
192+
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)
193+
194+
@Benchmark
195+
def transform_lazyZip(bh: Blackhole): Unit = {
196+
val xs1: IndexedSeq[Int] = xs
197+
bh.consume(xs1.lazyZip(xs1).map((_, _)))
198+
}
199+
200+
@Benchmark
201+
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)
179202
}

benchmarks/time/src/main/scala/strawman/collection/immutable/ScalaHashSetBenchmark.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import java.util.concurrent.TimeUnit
55
import org.openjdk.jmh.annotations._
66
import org.openjdk.jmh.infra.Blackhole
77

8-
import scala.{Any, AnyRef, Int, Long, Unit, math}
9-
import scala.Predef.intWrapper
8+
import scala.{Any, AnyRef, Int, Long, Tuple2, Unit, math}
9+
import scala.Predef.{intWrapper, $conforms, tuple2ToZippedOps}
1010

1111
@BenchmarkMode(scala.Array(Mode.AverageTime))
1212
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@@ -135,6 +135,21 @@ class ScalaHashSetBenchmark {
135135
}
136136
}
137137

138+
@Benchmark
139+
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))
140+
141+
@Benchmark
142+
def transform_zipMapTupled(bh: Blackhole): Unit = {
143+
val f = (a: Long, b: Long) => (a, b)
144+
bh.consume(xs.zip(xs).map(f.tupled))
145+
}
146+
147+
@Benchmark
148+
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)
149+
150+
@Benchmark
151+
def transform_lazyZip(bh: Blackhole): Unit = bh.consume((xs, xs).zipped.map((_, _)))
152+
138153
@Benchmark
139154
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip(t => (t._1, t._2)))
140155

benchmarks/time/src/main/scala/strawman/collection/immutable/ScalaListBenchmark.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import java.util.concurrent.TimeUnit
55
import org.openjdk.jmh.annotations._
66
import org.openjdk.jmh.infra.Blackhole
77

8-
import scala.{Any, AnyRef, Int, Long, Unit, math}
9-
import scala.Predef.{intWrapper, $conforms}
8+
import scala.{Any, AnyRef, Int, Long, Tuple2, Unit, math}
9+
import scala.Predef.{intWrapper, $conforms, tuple2ToZippedOps}
1010

1111
@BenchmarkMode(scala.Array(Mode.AverageTime))
1212
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@@ -270,6 +270,21 @@ class ScalaListBenchmark {
270270
}
271271
}
272272

273+
@Benchmark
274+
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))
275+
276+
@Benchmark
277+
def transform_zipMapTupled(bh: Blackhole): Unit = {
278+
val f = (a: Long, b: Long) => (a, b)
279+
bh.consume(xs.zip(xs).map(f.tupled))
280+
}
281+
282+
@Benchmark
283+
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)
284+
285+
@Benchmark
286+
def transform_lazyZip(bh: Blackhole): Unit = bh.consume((xs, xs).zipped.map((_, _)))
287+
273288
@Benchmark
274289
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)
275290

benchmarks/time/src/main/scala/strawman/collection/immutable/ScalaTreeSetBenchmark.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import java.util.concurrent.TimeUnit
55
import org.openjdk.jmh.annotations._
66
import org.openjdk.jmh.infra.Blackhole
77

8-
import scala.{Any, AnyRef, Int, Long, Unit, math}
9-
import scala.Predef.intWrapper
8+
import scala.{Any, AnyRef, Int, Long, Tuple2, Unit, math}
9+
import scala.Predef.{intWrapper, tuple2ToZippedOps, $conforms}
1010

1111
@BenchmarkMode(scala.Array(Mode.AverageTime))
1212
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@@ -135,6 +135,21 @@ class ScalaTreeSetBenchmark {
135135
}
136136
}
137137

138+
@Benchmark
139+
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))
140+
141+
@Benchmark
142+
def transform_zipMapTupled(bh: Blackhole): Unit = {
143+
val f = (a: Long, b: Long) => (a, b)
144+
bh.consume(xs.zip(xs).map(f.tupled))
145+
}
146+
147+
@Benchmark
148+
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)
149+
150+
@Benchmark
151+
def transform_lazyZip(bh: Blackhole): Unit = bh.consume((xs, xs).zipped.map((_, _)))
152+
138153
@Benchmark
139154
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip(t => (t._1, t._2)))
140155

0 commit comments

Comments
 (0)