Skip to content

Commit

Permalink
added fromPartialOrdering
Browse files Browse the repository at this point in the history
  • Loading branch information
kailuowang committed Dec 17, 2017
1 parent 6072552 commit 7c0b90d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,21 @@ class Tests extends FunSuite with Discipline {

checkAll("subsetPartialOrder[Int]", PartialOrderTests(subsetPartialOrder[Int]).partialOrder)

{
implicit def subsetPartialOrdering[A]: PartialOrdering[Set[A]] = new PartialOrdering[Set[A]] {

override def tryCompare(x: Set[A], y: Set[A]): Option[Int] = {
if (x == y) Some(0)
else if (x subsetOf y) Some(-1)
else if (y subsetOf x) Some(1)
else None
}

override def lteq(x: Set[A], y: Set[A]): Boolean = (x subsetOf y) || (x == y)
}
checkAll("fromPartialOrdering[Int]", PartialOrderTests(PartialOrder.fromPartialOrdering[Set[Int]]).partialOrder)
}

implicit val arbitraryComparison: Arbitrary[Comparison] =
Arbitrary(Gen.oneOf(Comparison.GreaterThan, Comparison.EqualTo, Comparison.LessThan))

Expand Down
3 changes: 3 additions & 0 deletions kernel/src/main/scala/cats/kernel/PartialOrder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ object PartialOrder extends PartialOrderFunctions[PartialOrder] with PartialOrde
def partialCompare(x: A, y: A) = f(x, y)
}

def fromPartialOrdering[A](implicit ev: PartialOrdering[A]): PartialOrder[A] = from[A] { (a, b) =>
ev.tryCompare(a, b).fold(Double.NaN)(_.toDouble)
}
}


Expand Down

0 comments on commit 7c0b90d

Please sign in to comment.