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

tailRecM improvements for ZIO and ZManaged #464

Merged
merged 1 commit into from
Nov 27, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ class CatsSpec extends ZioSpecBase {
}
)
checkAllAsync("GenSpawn[IO[Int, _], Int]", implicit tc => GenSpawnTests[IO[Int, _], Int].spawn[Int, Int, Int])
checkAllAsync("MonadError[IO[In t, _]]", implicit tc => MonadErrorTests[IO[Int, _], Int].monadError[Int, Int, Int])
checkAllAsync("MonadError[IO[Int, _]]", implicit tc => MonadErrorTests[IO[Int, _], Int].monadError[Int, Int, Int])
checkAllAsync("MonoidK[IO[Int, _]]", implicit tc => MonoidKTests[IO[Int, _]].monoidK[Int])
checkAllAsync("SemigroupK[IO[Option[Unit], _]]", implicit tc => SemigroupKTests[IO[Option[Unit], _]].semigroupK[Int])
checkAllAsync("SemigroupK[Task]", implicit tc => SemigroupKTests[Task].semigroupK[Int])
checkAllAsync("Bifunctor[IO]", implicit tc => BifunctorTests[IO].bifunctor[Int, Int, Int, Int, Int, Int])
checkAllAsync("Parallel[Task]", implicit tc => ParallelTests[Task, ParallelF[Task, _]].parallel[Int, Int])
checkAllAsync("Monad[URIO[Int, _]]", implicit tc => MonadTests[URIO[Int, _]].apply[Int, Int, Int])
checkAllAsync("Monad[URIO[Int, _]]", implicit tc => MonadTests[URIO[Int, _]].monad[Int, Int, Int])
checkAllAsync("Monad[URIO[Int, _]]", implicit tc => ExtraMonadTests[URIO[Int, _]].monadExtras[Int])
checkAllAsync(
"ArrowChoice[ZIO[_, Int, _]]",
implicit tc => ArrowChoiceTests[ZIO[_, Int, _]].arrowChoice[Int, Int, Int, Int, Int, Int]
Expand Down
11 changes: 10 additions & 1 deletion zio-interop-cats/shared/src/main/scala/zio/interop/cats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ private class ZioRuntimeAsync(implicit runtime: Runtime[Clock & CBlocking])
ZIO.never
}

private class ZioMonadError[R, E] extends MonadError[ZIO[R, E, _], E] with StackSafeMonad[ZIO[R, E, _]] {
private class ZioMonadError[R, E] extends MonadError[ZIO[R, E, _], E] {
type F[A] = ZIO[R, E, A]

override final def pure[A](a: A): F[A] =
Expand Down Expand Up @@ -437,6 +437,15 @@ private class ZioMonadError[R, E] extends MonadError[ZIO[R, E, _], E] with Stack

override final def adaptError[A](fa: F[A])(pf: PartialFunction[E, E]): F[A] =
fa.mapError(pf.orElse { case error => error })

override final def tailRecM[A, B](a: A)(f: A => F[Either[A, B]]): F[B] = {
def loop(a: A): F[B] = f(a).flatMap {
case Left(a) => loop(a)
case Right(b) => ZIO.succeedNow(b)
}

ZIO.effectSuspendTotal(loop(a))
}
}

private class ZioSemigroupK[R, E] extends SemigroupK[ZIO[R, E, _]] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,14 @@ private class ZManagedMonadError[R, E] extends MonadError[ZManaged[R, E, _], E]
override final def adaptError[A](fa: F[A])(pf: PartialFunction[E, E]): F[A] =
fa.mapError(pf.orElse { case error => error })

override final def tailRecM[A, B](a: A)(f: A => ZManaged[R, E, Either[A, B]]): ZManaged[R, E, B] =
ZManaged.suspend(f(a)).flatMap {
case Left(a) => tailRecM(a)(f)
override final def tailRecM[A, B](a: A)(f: A => F[Either[A, B]]): F[B] = {
def loop(a: A): F[B] = f(a).flatMap {
case Left(a) => loop(a)
case Right(b) => ZManaged.succeedNow(b)
}

ZManaged.suspend(loop(a))
}
}

private class ZManagedSemigroup[R, E, A](implicit semigroup: Semigroup[A]) extends Semigroup[ZManaged[R, E, A]] {
Expand Down