Sitelet https://github.com/typelevel/cats/commit/558beed7ba95048a18a5027b984916855363dac4
Skip to content

Commit 558beed

Browse files
committed
Use helper constructor for Order instances
1 parent 31bbd26 commit 558beed

10 files changed

Lines changed: 33 additions & 73 deletions

File tree

‎core/src/main/scala/cats/Eval.scala‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,7 @@ sealed abstract private[cats] class EvalInstances extends EvalInstances0 {
431431
}
432432

433433
implicit def catsOrderForEval[A: Order]: Order[Eval[A]] =
434-
new Order[Eval[A]] {
435-
def compare(lx: Eval[A], ly: Eval[A]): Int =
436-
Order[A].compare(lx.value, ly.value)
437-
}
434+
Order.by(_.value)
438435

439436
implicit def catsGroupForEval[A: Group]: Group[Eval[A]] =
440437
new EvalGroup[A] { val algebra: Group[A] = Group[A] }

‎core/src/main/scala/cats/data/Const.scala‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ sealed abstract private[data] class ConstInstances extends ConstInstances0 {
9191
}
9292

9393
implicit def catsDataOrderForConst[A: Order, B]: Order[Const[A, B]] =
94-
new Order[Const[A, B]] {
95-
def compare(x: Const[A, B], y: Const[A, B]): Int =
96-
x.compare(y)
97-
}
94+
Order.from(_ compare _)
9895

9996
implicit def catsDataAlignForConst[A: Semigroup]: Align[Const[A, *]] =
10097
new Align[Const[A, *]] {

‎core/src/main/scala/cats/data/Ior.scala‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -858,10 +858,7 @@ sealed abstract private[data] class IorInstances extends IorInstances0 {
858858
}
859859

860860
implicit def catsDataOrderForIor[A: Order, B: Order]: Order[A Ior B] =
861-
new Order[A Ior B] {
862-
863-
def compare(x: Ior[A, B], y: Ior[A, B]): Int = x.compare(y)
864-
}
861+
Order.from(_ compare _)
865862

866863
implicit def catsDataShowForIor[A: Show, B: Show]: Show[A Ior B] =
867864
Show.show(_.show)

‎core/src/main/scala/cats/data/WriterT.scala‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,7 @@ sealed abstract private[data] class WriterTInstances1 extends WriterTInstances2
403403
catsDataFoldableForWriterT[Id, L](F)
404404

405405
implicit def catsDataOrderForWriterT[F[_], L, V](implicit Ord: Order[F[(L, V)]]): Order[WriterT[F, L, V]] =
406-
new Order[WriterT[F, L, V]] {
407-
def compare(x: WriterT[F, L, V], y: WriterT[F, L, V]): Int = x.compare(y)
408-
}
406+
Order.from(_ compare _)
409407
}
410408

411409
sealed abstract private[data] class WriterTInstances2 extends WriterTInstances3 {

‎core/src/main/scala/cats/instances/order.scala‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ trait OrderInstances extends kernel.instances.OrderInstances {
4343
Order.by(f)(fa)
4444

4545
def product[A, B](fa: Order[A], fb: Order[B]): Order[(A, B)] =
46-
new Order[(A, B)] {
47-
def compare(x: (A, B), y: (A, B)): Int = {
48-
val z = fa.compare(x._1, y._1)
49-
if (z == 0) fb.compare(x._2, y._2) else z
50-
}
46+
Order.from { (x, y) =>
47+
val z = fa.compare(x._1, y._1)
48+
if (z == 0) fb.compare(x._2, y._2) else z
5149
}
5250
}
5351
}

‎kernel/src/main/scala/cats/kernel/Order.scala‎

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ trait Order[@sp A] extends Any with PartialOrder[A] { self =>
115115
* instance.
116116
*/
117117
def toOrdering: Ordering[A] =
118-
new Ordering[A] {
119-
def compare(x: A, y: A): Int = self.compare(x, y)
120-
}
118+
compare(_, _)
121119
}
122120

123121
abstract class OrderFunctions[O[T] <: Order[T]] extends PartialOrderFunctions[O] {
@@ -158,17 +156,13 @@ object Order extends OrderFunctions[Order] with OrderToOrderingConversion {
158156
* function `f`.
159157
*/
160158
def by[@sp A, @sp B](f: A => B)(implicit ev: Order[B]): Order[A] =
161-
new Order[A] {
162-
def compare(x: A, y: A): Int = ev.compare(f(x), f(y))
163-
}
159+
from((x, y) => ev.compare(f(x), f(y)))
164160

165161
/**
166162
* Defines an ordering on `A` from the given order such that all arrows switch direction.
167163
*/
168164
def reverse[@sp A](order: Order[A]): Order[A] =
169-
new Order[A] {
170-
def compare(x: A, y: A): Int = order.compare(y, x)
171-
}
165+
from((x, y) => order.compare(y, x))
172166

173167
/**
174168
* Returns a new `Order[A]` instance that first compares by the first
@@ -178,21 +172,17 @@ object Order extends OrderFunctions[Order] with OrderToOrderingConversion {
178172
* then (if two elements are equal) falls back to `y` for the comparison.
179173
*/
180174
def whenEqual[@sp A](first: Order[A], second: Order[A]): Order[A] =
181-
new Order[A] {
182-
def compare(x: A, y: A) = {
183-
val c = first.compare(x, y)
184-
if (c == 0) second.compare(x, y)
185-
else c
186-
}
175+
from { (x, y) =>
176+
val c = first.compare(x, y)
177+
if (c == 0) second.compare(x, y)
178+
else c
187179
}
188180

189181
/**
190182
* Define an `Order[A]` using the given function `f`.
191183
*/
192184
def from[@sp A](f: (A, A) => Int): Order[A] =
193-
new Order[A] {
194-
def compare(x: A, y: A) = f(x, y)
195-
}
185+
f(_, _)
196186

197187
/**
198188
* Define an `Order[A]` using the given 'less than' function `f`.
@@ -215,9 +205,7 @@ object Order extends OrderFunctions[Order] with OrderToOrderingConversion {
215205
* An `Order` instance that considers all `A` instances to be equal.
216206
*/
217207
def allEqual[A]: Order[A] =
218-
new Order[A] {
219-
def compare(x: A, y: A): Int = 0
220-
}
208+
from((_, _) => 0)
221209

222210
/**
223211
* A `Monoid[Order[A]]` can be generated for all `A` with the following
@@ -248,8 +236,5 @@ object Order extends OrderFunctions[Order] with OrderToOrderingConversion {
248236
}
249237

250238
def fromComparable[A <: Comparable[A]]: Order[A] =
251-
new Order[A] {
252-
override def compare(x: A, y: A): Int =
253-
x.compareTo(y)
254-
}
239+
from(_ compareTo _)
255240
}

‎kernel/src/main/scala/cats/kernel/instances/EitherInstances.scala‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@ package instances
2525
trait EitherInstances extends EitherInstances0 {
2626

2727
implicit def catsStdOrderForEither[A, B](implicit A: Order[A], B: Order[B]): Order[Either[A, B]] =
28-
new Order[Either[A, B]] {
29-
def compare(x: Either[A, B], y: Either[A, B]): Int =
30-
x match {
31-
case Left(xx) =>
32-
y match {
33-
case Left(yy) => A.compare(xx, yy)
34-
case Right(_) => -1
35-
}
36-
case Right(xx) =>
37-
y match {
38-
case Left(_) => 1
39-
case Right(yy) => B.compare(xx, yy)
40-
}
41-
}
28+
Order.from { (x, y) =>
29+
x match {
30+
case Left(xx) =>
31+
y match {
32+
case Left(yy) => A.compare(xx, yy)
33+
case Right(_) => -1
34+
}
35+
case Right(xx) =>
36+
y match {
37+
case Left(_) => 1
38+
case Right(yy) => B.compare(xx, yy)
39+
}
40+
}
4241
}
4342

4443
implicit def catsDataMonoidForEither[A, B](implicit B: Monoid[B]): Monoid[Either[A, B]] =

‎kernel/src/main/scala/cats/kernel/instances/FunctionInstances.scala‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ import scala.util.control.TailCalls.{done, tailcall, TailRec}
2929
trait FunctionInstances extends FunctionInstances0 {
3030

3131
implicit def catsKernelOrderForFunction0[A](implicit ev: Order[A]): Order[() => A] =
32-
new Order[() => A] {
33-
def compare(x: () => A, y: () => A): Int = ev.compare(x(), y())
34-
}
32+
Order.by(_.apply())
3533

3634
implicit def catsKernelCommutativeGroupForFunction0[A](implicit G: CommutativeGroup[A]): CommutativeGroup[() => A] =
3735
new Function0Group[A] with CommutativeGroup[() => A] { def A: Group[A] = G }

‎laws/src/main/scala/cats/laws/discipline/arbitrary.scala‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,7 @@ object arbitrary extends ArbitraryInstances0 with ScalaVersionSpecific.Arbitrary
294294
Arbitrary(getArbitrary[PartialOrder[A]].map(PartialOrder.catsKernelPartialOrderingForPartialOrder(_)))
295295

296296
implicit def catsLawsArbitraryForOrder[A: Arbitrary]: Arbitrary[Order[A]] =
297-
Arbitrary(
298-
getArbitrary[Int => Int].map(f =>
299-
new Order[A] {
300-
def compare(x: A, y: A): Int = java.lang.Integer.compare(f(x.##), f(y.##))
301-
}
302-
)
303-
)
297+
Arbitrary(getArbitrary[Int => Int].map(f => Order.from((x, y) => Integer.compare(f(x.##), f(y.##)))))
304298

305299
implicit def catsLawsArbitraryForSortedMap[K: Arbitrary: Order, V: Arbitrary]: Arbitrary[SortedMap[K, V]] =
306300
Arbitrary(getArbitrary[Map[K, V]].map(s => SortedMap.empty[K, V](implicitly[Order[K]].toOrdering) ++ s))

‎tests/js/src/test/scala/cats/js/instances/future.scala‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ sealed private[instances] trait FutureInstances0 extends FutureInstances1 {
4848
}
4949

5050
def futureOrder[A: Order](atMost: FiniteDuration)(implicit ec: E): Order[Future[A]] =
51-
new Order[Future[A]] {
52-
def compare(x: Future[A], y: Future[A]): Int =
53-
Await.result(x.zip(y).map { case (x, y) => x.compare(y) }, atMost)
54-
}
51+
Order.from((x, y) => Await.result(x.zip(y).map { case (x, y) => x.compare(y) }, atMost))
5552
}
5653

5754
sealed private[instances] trait FutureInstances1 extends FutureInstances2 {

0 commit comments

Comments
 (0)