Skip to content

Commit

Permalink
Merge pull request #1354 from vectos/fristi/add-ior-monoid
Browse files Browse the repository at this point in the history
Add semigroup instance for Ior
  • Loading branch information
non authored Sep 5, 2016
2 parents 01e7444 + 4ff06d4 commit ae51ce5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/src/main/scala/cats/data/Ior.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion tests/src/test/scala/cats/tests/IorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand All @@ -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)
Expand Down

0 comments on commit ae51ce5

Please sign in to comment.