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

Chunk - Add optimised traverse_ for StackSafeMonad. #3191

Merged
merged 1 commit into from
Mar 27, 2023
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
14 changes: 12 additions & 2 deletions core/shared/src/main/scala/fs2/Chunk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import scala.reflect.ClassTag
import scodec.bits.{BitVector, ByteVector}
import java.nio.{Buffer => JBuffer, ByteBuffer => JByteBuffer, CharBuffer => JCharBuffer}

import cats.{Alternative, Applicative, Eq, Eval, Monad, Monoid, Traverse, TraverseFilter}
import cats._
import cats.data.{Chain, NonEmptyList}
import cats.syntax.all._

Expand Down Expand Up @@ -1232,7 +1232,17 @@ object Chunk
fa.size match {
case 0 => F.unit
case 1 => f(fa(0)).void
case _ => super.traverse_(fa)(f)
case _ =>
F match {
case sF: StackSafeMonad[F] =>
def go(ix: Int): F[Unit] =
if (ix < fa.size)
sF.flatMap(f(fa(ix)))(_ => go(ix + 1))
else sF.unit
go(0)
case _ =>
super.traverse_(fa)(f)
}
}

override def tailRecM[A, B](a: A)(f: A => Chunk[Either[A, B]]): Chunk[B] = {
Expand Down