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

Add Applicative IorT instance #2181

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 22 additions & 1 deletion core/src/main/scala/cats/data/IorT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,15 @@ private[data] abstract class IorTInstances2 extends IorTInstances3 {
new IorTEq[F, A, B] { val F0: Eq[F[Ior[A, B]]] = F }
}

private[data] abstract class IorTInstances3 {
private[data] abstract class IorTInstances3 extends IorTInstances4 {
implicit def catsDataApplicativeForIorT[F[_], A](implicit F: Applicative[F], A: Semigroup[A]): Applicative[IorT[F, A, ?]] =
new IorTApplicative[F, A] {
val F0: Applicative[F] = F
val A0: Semigroup[A] = A
}
}

private[data] abstract class IorTInstances4 {
implicit def catsDataFunctorForIorT[F[_], A](implicit F: Functor[F]): Functor[IorT[F, A, ?]] =
new IorTFunctor[F, A] { val F0: Functor[F] = F }
}
Expand All @@ -493,6 +501,19 @@ private[data] sealed trait IorTFunctor[F[_], A] extends Functor[IorT[F, A, ?]] {
override def map[B, D](iort: IorT[F, A, B])(f: B => D): IorT[F, A, D] = iort.map(f)
}

private[data] sealed trait IorTApplicative[F[_], A] extends Applicative[IorT[F, A, ?]] {
implicit def F0: Applicative[F]
implicit def A0: Semigroup[A]

override def map[B, D](iort: IorT[F, A, B])(f: B => D): IorT[F, A, D] = iort.map(f)

override def ap[B, C](ff: IorT[F, A, B => C])(fa: IorT[F, A, B]): IorT[F, A, C] =
IorT(F0.compose[Ior[A, ?]].ap(ff.value)(fa.value))

override def pure[B](x: B): IorT[F, A, B] =
IorT(F0.pure(Ior.Right(x)))
}

private[data] sealed trait IorTEq[F[_], A, B] extends Eq[IorT[F, A, B]] {
implicit def F0: Eq[F[Ior[A, B]]]

Expand Down
7 changes: 7 additions & 0 deletions tests/src/test/scala/cats/tests/IorTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class IorTSuite extends CatsSuite {
checkAll("Traverse[IorT[ListWrapper, Int, ?]]", SerializableTests.serializable(Traverse[IorT[ListWrapper, Int, ?]]))
}

{
implicit val F = ListWrapper.applicative

checkAll("IorT[ListWrapper, String, Int]", ApplicativeTests[IorT[ListWrapper, String, ?]].applicative[Int, Int, Int])
checkAll("Applicative[IorT[List, ?, ?]]", SerializableTests.serializable(Applicative[IorT[ListWrapper, String, ?]]))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description says List but the code uses ListWrapper. Is this intended ?

}

{
implicit val F = ListWrapper.monad

Expand Down