From d9c3c5b86060efccd0f7f1c111eb37ef9f176a35 Mon Sep 17 00:00:00 2001 From: Cody Allen Date: Thu, 14 Dec 2017 02:54:03 -0800 Subject: [PATCH] Add Commutative{Monad, FlatMap} instances for IdT (#2104) --- core/src/main/scala/cats/data/IdT.scala | 12 +++++++++++- tests/src/test/scala/cats/tests/IdTSuite.scala | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/data/IdT.scala b/core/src/main/scala/cats/data/IdT.scala index 7a79b014dc..9e555b547a 100644 --- a/core/src/main/scala/cats/data/IdT.scala +++ b/core/src/main/scala/cats/data/IdT.scala @@ -135,7 +135,17 @@ private[data] sealed trait IdTNonEmptyTraverse[F[_]] extends IdTTraverse[F] with fa.reduceRightTo(f)(g) } -private[data] sealed abstract class IdTInstances6 { +private[data] sealed abstract class IdTInstances8 { + implicit def catsDataCommutativeFlatMapForIdT[F[_]](implicit F: CommutativeFlatMap[F]): CommutativeFlatMap[IdT[F, ?]] = + new IdTFlatMap[F] with CommutativeFlatMap[IdT[F, ?]] { implicit val F0: CommutativeFlatMap[F] = F } +} + +private[data] sealed abstract class IdTInstances7 extends IdTInstances8{ + implicit def catsDataCommutativeMonadForIdT[F[_]](implicit F: CommutativeMonad[F]): CommutativeMonad[IdT[F, ?]] = + new IdTMonad[F] with CommutativeMonad[IdT[F, ?]] { implicit val F0: CommutativeMonad[F] = F } +} + +private[data] sealed abstract class IdTInstances6 extends IdTInstances7 { implicit def catsDataContravariantMonoidalForIdT[F[_]](implicit F: ContravariantMonoidal[F]): ContravariantMonoidal[IdT[F, ?]] = new IdTContravariantMonoidal[F] { implicit val F0: ContravariantMonoidal[F] = F } } diff --git a/tests/src/test/scala/cats/tests/IdTSuite.scala b/tests/src/test/scala/cats/tests/IdTSuite.scala index 770d753599..878590d363 100644 --- a/tests/src/test/scala/cats/tests/IdTSuite.scala +++ b/tests/src/test/scala/cats/tests/IdTSuite.scala @@ -5,11 +5,18 @@ import cats.data.{Const, IdT, NonEmptyList} import cats.kernel.laws.discipline.{OrderTests, EqTests} import cats.laws.discipline._ import cats.laws.discipline.arbitrary._ +import Helpers.CSemi class IdTSuite extends CatsSuite { implicit val iso = SemigroupalTests.Isomorphisms.invariant[IdT[ListWrapper, ?]](IdT.catsDataFunctorForIdT(ListWrapper.functor)) + checkAll("IdT[(CSemi, ?), Int]", CommutativeFlatMapTests[IdT[(CSemi, ?), ?]].commutativeFlatMap[Int, Int, Int]) + checkAll("CommutativeFlatMap[IdT[(CSemi, ?), ?]]", SerializableTests.serializable(CommutativeFlatMap[IdT[(CSemi, ?), ?]])) + + checkAll("IdT[Option, Int]", CommutativeMonadTests[IdT[Option, ?]].commutativeMonad[Int, Int, Int]) + checkAll("CommutativeMonad[IdT[Option, ?]]", SerializableTests.serializable(CommutativeMonad[IdT[Option, ?]])) + { implicit val F = ListWrapper.eqv[Option[Int]]