Skip to content

Commit

Permalink
Adds Semigroup for Xor
Browse files Browse the repository at this point in the history
As per typelevel#716, adding a Semigroup for the Xor data type which currently
has a Monoid instance but no Semigroup instance.
  • Loading branch information
mikejcurry committed Dec 6, 2015
1 parent 48c38e5 commit b7dd217
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/Xor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ private[data] sealed abstract class XorInstances extends XorInstances1 {
}

private[data] sealed abstract class XorInstances1 extends XorInstances2 {

implicit def xorSemigroup[A, B](implicit A: Semigroup[A], B: Semigroup[B]): Semigroup[A Xor B] =
new Semigroup[A Xor B] {
def combine(x: A Xor B, y: A Xor B): A Xor B = x combine y
}

implicit def xorPartialOrder[A: PartialOrder, B: PartialOrder]: PartialOrder[A Xor B] = new PartialOrder[A Xor B] {
def partialCompare(x: A Xor B, y: A Xor B): Double = x partialCompare y
override def eqv(x: A Xor B, y: A Xor B): Boolean = x === y
Expand Down
7 changes: 4 additions & 3 deletions tests/src/test/scala/cats/tests/XorTests.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cats
package tests

import cats.data.{Xor, XorT}
import cats.data.{NonEmptyList, Xor, XorT}
import cats.data.Xor._
import cats.laws.discipline.arbitrary.xorArbitrary
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.{BifunctorTests, TraverseTests, MonadErrorTests, SerializableTests}
import algebra.laws.{GroupLaws, OrderLaws}
import org.scalacheck.{Arbitrary, Gen}
Expand All @@ -12,7 +12,8 @@ import org.scalacheck.Arbitrary._
import scala.util.Try

class XorTests extends CatsSuite {
checkAll("Xor[String, Int]", GroupLaws[Xor[String, Int]].monoid)
checkAll("Monoid[Xor[String, Int]]", GroupLaws[Xor[String, Int]].monoid)
checkAll("Semigroup[Xor[String, NonEmptyList[Int]]]", GroupLaws[Xor[String, NonEmptyList[Int]]].semigroup)

implicit val eq0 = XorT.xorTEq[Xor[String, ?], String, Int]

Expand Down

0 comments on commit b7dd217

Please sign in to comment.