-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add syntax for Eq, Order, and PartialOrder.
As I said previously, Algebra currently provides no syntax so we get to do that here.
- Loading branch information
Showing
5 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |