Skip to content

Commit

Permalink
Merge pull request #521 from drostron/xor-monoid-instance
Browse files Browse the repository at this point in the history
add xorMonoid
  • Loading branch information
non committed Sep 10, 2015
2 parents eb5f431 + 86dbe1c commit a89d71e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion core/src/main/scala/cats/data/Xor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,13 @@ sealed abstract class XorInstances extends XorInstances1 {
def show(f: A Xor B): String = f.show
}

implicit def xorInstances[A]: Traverse[A Xor ?] with MonadError[Xor, A ]=
implicit def xorMonoid[A, B](implicit A: Semigroup[A], B: Monoid[B]): Monoid[A Xor B] =
new Monoid[A Xor B] {
def empty: A Xor B = Xor.Right(B.empty)
def combine(x: A Xor B, y: A Xor B): A Xor B = x combine y
}

implicit def xorInstances[A]: Traverse[A Xor ?] with MonadError[Xor, A ] =
new Traverse[A Xor ?] with MonadError[Xor, A] {
def traverse[F[_]: Applicative, B, C](fa: A Xor B)(f: B => F[C]): F[A Xor C] = fa.traverse(f)
def foldLeft[B, C](fa: A Xor B, c: C)(f: (C, B) => C): C = fa.foldLeft(c)(f)
Expand Down
3 changes: 3 additions & 0 deletions tests/shared/src/test/scala/cats/tests/XorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests

import cats.data.Xor
import cats.data.Xor._
import cats.laws.discipline.arbitrary.xorArbitrary
import cats.laws.discipline.{TraverseTests, MonadErrorTests, SerializableTests}
import org.scalacheck.{Arbitrary, Gen}
import org.scalacheck.Prop._
Expand All @@ -12,6 +13,8 @@ import org.scalacheck.Arbitrary._
import scala.util.Try

class XorTests extends CatsSuite {
checkAll("Xor[String, Int]", algebra.laws.GroupLaws[Xor[String, Int]].monoid)

checkAll("Xor[String, Int]", MonadErrorTests[Xor, String].monadError[Int, Int, Int])
checkAll("MonadError[Xor, String]", SerializableTests.serializable(MonadError[Xor, String]))

Expand Down

0 comments on commit a89d71e

Please sign in to comment.