diff --git a/core/src/main/scala/cats/data/Ior.scala b/core/src/main/scala/cats/data/Ior.scala index 647e7b8bd3..4adf44d61d 100644 --- a/core/src/main/scala/cats/data/Ior.scala +++ b/core/src/main/scala/cats/data/Ior.scala @@ -140,6 +140,10 @@ private[data] sealed abstract class IorInstances extends IorInstances0 { def show(f: A Ior B): String = f.show } + implicit def catsDataSemigroupForIor[A: Semigroup, B: Semigroup]: Semigroup[Ior[A, B]] = new Semigroup[Ior[A, B]] { + def combine(x: Ior[A, B], y: Ior[A, B]) = x.append(y) + } + implicit def catsDataMonadForIor[A: Semigroup]: Monad[A Ior ?] with RecursiveTailRecM[A Ior ?] = new Monad[A Ior ?] with RecursiveTailRecM[A Ior ?] { def pure[B](b: B): A Ior B = Ior.right(b) def flatMap[B, C](fa: A Ior B)(f: B => A Ior C): A Ior C = fa.flatMap(f) diff --git a/tests/src/test/scala/cats/tests/IorTests.scala b/tests/src/test/scala/cats/tests/IorTests.scala index ae9a797c36..e56013ded2 100644 --- a/tests/src/test/scala/cats/tests/IorTests.scala +++ b/tests/src/test/scala/cats/tests/IorTests.scala @@ -2,7 +2,8 @@ package cats package tests import cats.data.Ior -import cats.laws.discipline.{BifunctorTests, TraverseTests, MonadTests, SerializableTests, CartesianTests} +import cats.kernel.laws.GroupLaws +import cats.laws.discipline.{BifunctorTests, CartesianTests, MonadTests, SerializableTests, TraverseTests} import cats.laws.discipline.arbitrary._ import org.scalacheck.Arbitrary._ @@ -20,6 +21,9 @@ class IorTests extends CatsSuite { checkAll("Traverse[String Ior ?]", SerializableTests.serializable(Traverse[String Ior ?])) checkAll("? Ior ?", BifunctorTests[Ior].bifunctor[Int, Int, Int, String, String, String]) + checkAll("Semigroup[Ior[A: Semigroup, B: Semigroup]]", GroupLaws[Ior[List[Int], List[Int]]].semigroup) + checkAll("SerializableTest Semigroup[Ior[A: Semigroup, B: Semigroup]]", SerializableTests.serializable(Semigroup[Ior[List[Int], List[Int]]])) + test("left Option is defined left and both") { forAll { (i: Int Ior String) => (i.isLeft || i.isBoth) should === (i.left.isDefined)