Skip to content

Commit

Permalink
Add syntax for Eq, Order, and PartialOrder.
Browse files Browse the repository at this point in the history
As I said previously, Algebra currently provides no syntax
so we get to do that here.
  • Loading branch information
non committed Feb 5, 2015
1 parent 62251dc commit f2a007b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ trait AllSyntax
with CoFlatMapSyntax
with ComonadSyntax
with ContravariantSyntax
with EqSyntax
with FlatMapSyntax
with FoldableSyntax
with FunctorSyntax
with InvariantSyntax
with MonadCombineSyntax
with MonadFilterSyntax
with OrderSyntax
with PartialOrderSyntax
with ProfunctorSyntax
with SemigroupSyntax
with ShowSyntax
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/scala/cats/syntax/eq.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cats
package syntax

trait EqSyntax {
// TODO: use simulacrum instances eventually
implicit def eqSyntax[A: Eq](a: A) =
new EqOps[A](a)
}

class EqOps[A](lhs: A)(implicit A: Eq[A]) {
def ===(rhs: A): Boolean = A.eqv(lhs, rhs)
def =!=(rhs: A): Boolean = A.neqv(lhs, rhs)
}
14 changes: 14 additions & 0 deletions core/src/main/scala/cats/syntax/order.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cats
package syntax

trait OrderSyntax {
// TODO: use simulacrum instances eventually
implicit def orderSyntax[A: Order](a: A) =
new OrderOps[A](a)
}

class OrderOps[A](lhs: A)(implicit A: Order[A]) {
def compare(rhs: A): Int = A.compare(lhs, rhs)
def min(rhs: A): A = A.min(lhs, rhs)
def max(rhs: A): A = A.max(lhs, rhs)
}
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ package object syntax {
object coflatMap extends CoFlatMapSyntax
object comonad extends ComonadSyntax
object contravariant extends ContravariantSyntax
object eq extends EqSyntax
object flatMap extends FlatMapSyntax
object foldable extends FoldableSyntax
object functor extends FunctorSyntax
object invariant extends InvariantSyntax
object monadCombine extends MonadCombineSyntax
object monadFilter extends MonadFilterSyntax
object order extends OrderSyntax
object partialOrder extends PartialOrderSyntax
object profunctor extends ProfunctorSyntax
object semigroup extends SemigroupSyntax
object show extends ShowSyntax
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/scala/cats/syntax/partialOrder.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cats
package syntax

trait PartialOrderSyntax {
// TODO: use simulacrum instances eventually
implicit def partialOrderSyntax[A: PartialOrder](a: A) =
new PartialOrderOps[A](a)
}

class PartialOrderOps[A](lhs: A)(implicit A: PartialOrder[A]) {
def >(rhs: A): Boolean = A.gt(lhs, rhs)
def >=(rhs: A): Boolean = A.gteqv(lhs, rhs)
def <(rhs: A): Boolean = A.lt(lhs, rhs)
def <=(rhs: A): Boolean = A.lteqv(lhs, rhs)

def partialCompare(rhs: A): Double = A.partialCompare(lhs, rhs)
def tryCompare(rhs: A): Option[Int] = A.tryCompare(lhs, rhs)
def pmin(rhs: A): Option[A] = A.pmin(lhs, rhs)
def pmax(rhs: A): Option[A] = A.pmax(lhs, rhs)
}

0 comments on commit f2a007b

Please sign in to comment.