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

Override and short-circuit Chunk.flatten #2990

Merged
merged 1 commit into from
Sep 22, 2022
Merged
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
9 changes: 9 additions & 0 deletions core/shared/src/main/scala/fs2/Chunk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,15 @@ object Chunk
override def pure[A](a: A): Chunk[A] = Chunk.singleton(a)
override def map[A, B](fa: Chunk[A])(f: A => B): Chunk[B] = fa.map(f)
override def flatMap[A, B](fa: Chunk[A])(f: A => Chunk[B]): Chunk[B] = fa.flatMap(f)
override def flatten[A](ffa: Chunk[Chunk[A]]): Chunk[A] =
if (ffa.isEmpty) Chunk.empty
else if (ffa.size == 1) ffa(0) // short-circuit and simply return the first chunk
else {
var acc = Chunk.Queue.empty[A]
ffa.foreach(x => acc = acc :+ x)
acc
}

override def tailRecM[A, B](a: A)(f: A => Chunk[Either[A, B]]): Chunk[B] = {
// Based on the implementation of tailRecM for Vector from cats, licensed under MIT
val buf = makeArrayBuilder[Any]
Expand Down