-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Introduce cats-kernel and remove algebra dependency #1001
Changes from 2 commits
03aad13
38a3dc8
157710f
ceb94cc
673d045
d19a098
3378af3
34e206a
041a06a
f8e3910
e6bd8fe
4b8b789
ada6b75
de8a2fc
fc0acf2
5f408e1
d2dcb23
8fe8938
aac4652
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,11 +25,24 @@ trait Group[@sp(Int, Long, Float, Double) A] extends Any with Monoid[A] { | |
* Return `a` appended to itself `n` times. If `n` is negative, then | ||
* this returns `inverse(a)` appended to itself `n` times. | ||
*/ | ||
override def combineN(a: A, n: Int): A = | ||
override def combineN(a: A, n: Int): A = { | ||
// This method is a bit tricky. Normally, to sum x a negative | ||
// number of times (n), we can sum (-x) a positive number of times | ||
// (-n). The issue here is that Int.MinValue cannot be negated; in | ||
// other words (-MinValue) == MinValue. | ||
// | ||
// To work around that, we rely on the fact that we can divide n | ||
// by 2 if we sum 2a (i.e. combine(a, a)) instead. Here is the | ||
// transformation we use: | ||
// | ||
// combineN(x, -2147483648) | ||
// combineN(combine(x, x), -1073741824) | ||
// combineN(inverse(combine(x, x)), 1073741824) | ||
if (n > 0) repeatedCombineN(a, n) | ||
else if (n == 0) empty | ||
else if (n == Int.MinValue) combineN(inverse(combine(a, a)), 1073741824) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is due to:
So, Can we add some verbosity to the comments? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. |
||
else repeatedCombineN(inverse(a), -n) | ||
} | ||
} | ||
|
||
trait GroupFunctions[G[T] <: Group[T]] extends MonoidFunctions[Group] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as https://github.com/typelevel/cats/pull/1001/files#r61071186 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,13 +177,14 @@ object Order extends OrderFunctions { | |
/** | ||
* An `Order` instance that considers all `A` instances to be equal. | ||
*/ | ||
def allEqual[A]: Order[A] = new Order[A] { | ||
def compare(x: A, y: A): Int = 0 | ||
} | ||
def allEqual[A]: Order[A] = | ||
new Order[A] { | ||
def compare(x: A, y: A): Int = 0 | ||
} | ||
|
||
|
||
/** | ||
* A `Monoid[Order[A]]` can be generated for all `A` with the following | ||
* A `Band[Order[A]]` can be generated for all `A` with the following | ||
* properties: | ||
* | ||
* `empty` returns a trivial `Order[A]` which considers all `A` instances to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Warning nitpicky comment: |
||
|
@@ -194,16 +195,16 @@ object Order extends OrderFunctions { | |
* | ||
* @see [[Order.whenEqual]] | ||
*/ | ||
def whenEqualMonoid[A]: Monoid[Order[A]] = | ||
new Monoid[Order[A]] { | ||
def whenEqualMonoid[A]: Band[Order[A]] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want |
||
new Band[Order[A]] { | ||
val empty: Order[A] = allEqual[A] | ||
|
||
def combine(x: Order[A], y: Order[A]): Order[A] = x whenEqual y | ||
} | ||
|
||
def fromOrdering[A](implicit ev: Ordering[A]): Order[A] = new Order[A] { | ||
def compare(x: A, y: A): Int = ev.compare(x, y) | ||
def fromOrdering[A](implicit ev: Ordering[A]): Order[A] = | ||
new Order[A] { | ||
def compare(x: A, y: A): Int = ev.compare(x, y) | ||
|
||
override def toOrdering: Ordering[A] = ev | ||
} | ||
override def toOrdering: Ordering[A] = ev | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,15 @@ package object bigInt extends BigIntInstances | |
trait BigIntInstances { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a |
||
implicit val bigIntOrder: Order[BigInt] = | ||
new BigIntOrder | ||
implicit val bigIntGroup: CommutativeGroup[BigInt] = | ||
new BigIntGroup | ||
} | ||
|
||
class BigIntGroup extends CommutativeGroup[BigInt] { | ||
val empty: BigInt = BigInt(0) | ||
def combine(x: BigInt, y: BigInt): BigInt = x + y | ||
def inverse(x: BigInt): BigInt = -x | ||
override def remove(x: BigInt, y: BigInt): BigInt = x - y | ||
} | ||
|
||
class BigIntOrder extends Order[BigInt] { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,14 @@ import java.lang.Math | |
|
||
trait DoubleInstances { | ||
implicit val doubleOrder: Order[Double] = new DoubleOrder | ||
implicit val doubleGroup: CommutativeGroup[Double] = new DoubleGroup | ||
} | ||
|
||
class DoubleGroup extends CommutativeGroup[Double] { | ||
def combine(x: Double, y: Double): Double = x + y | ||
def empty: Double = 0D | ||
def inverse(x: Double): Double = -x | ||
override def remove(x: Double, y: Double): Double = x - y | ||
} | ||
|
||
class DoubleOrder extends Order[Double] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. strictly, is this a PartialOrder due to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation is using
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this used for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That may not be necessary -- I'm not sure what we're using it for. I'll try removing it.