Skip to content
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

adds zipWithIndex function on Chunk #1859

Merged
merged 4 commits into from
May 5, 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: 18 additions & 0 deletions core/shared/src/main/scala/fs2/Chunk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,24 @@ abstract class Chunk[+O] extends Serializable { self =>
Chunk.array(arr.asInstanceOf[Array[O3]])
}

/**
* Zips the elements of the input chunk with its indices, and returns the new chunk.
*
* @example {{{
* scala> Chunk("The", "quick", "brown", "fox").zipWithIndex.toList
* res0: List[(String, Int)] = List((The,0), (quick,1), (brown,2), (fox,3))
* }}}
*/
def zipWithIndex: Chunk[(O, Int)] = {
val arr = new Array[(O, Int)](size)
var i = 0
while (i < size) {
arr(i) = (apply(i), i)
i += 1
}
Chunk.array(arr)
}

override def hashCode: Int = {
import util.hashing.MurmurHash3
var i = 0
Expand Down
4 changes: 4 additions & 0 deletions core/shared/src/test/scala/fs2/ChunkSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ class ChunkSpec extends Fs2Spec {
assert(arr2 === Array(0, 0))
}

"zipWithIndex andThen toArray" in {
forAll((chunk: Chunk[Int]) => assert(chunk.zipWithIndex.toArray === chunk.toArray.zipWithIndex))
}

"Boxed toArray - regression #1745" in {
Chunk.Boxed(Array[Any](0)).asInstanceOf[Chunk[Int]].toArray[Any]
Chunk.Boxed(Array[Any](0)).asInstanceOf[Chunk[Int]].toArray[Int]
Expand Down