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 xorMonoid #521

Merged
merged 1 commit into from
Sep 10, 2015
Merged
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Check Serializability

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see - Xor[String, Int].monoid.serializable in the test output. It appears serializability is checked via:

https://github.com/non/algebra/blob/v0.3.1/laws/shared/src/main/scala/algebra/laws/GroupLaws.scala#L28

Is that sufficient or is there a reason to have the following in addition?

checkAll("Xor[String, Int]", SerializableTests.serializable(Monoid[Xor[String, Int]]))

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh you're right, algebra includes Serializable checks in their laws - @non confirm?


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

Expand Down