Sitelet https://github.com/typelevel/cats/commit/826cc56ea113323bd9ba76b8b34cd2218f0a9c21
Skip to content

Commit 826cc56

Browse files
committed
Convert Order and PartialOrder instances to SAM syntax
1 parent 9a8fa23 commit 826cc56

12 files changed

Lines changed: 73 additions & 89 deletions

File tree

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ sealed abstract private[data] class ConstInstances extends ConstInstances0 {
9090
override def minBound: Const[A, B] = Const(A.minBound)
9191
}
9292

93-
implicit def catsDataOrderForConst[A: Order, B]: Order[Const[A, B]] =
94-
Order.from(_ compare _)
93+
implicit def catsDataOrderForConst[A: Order, B]: Order[Const[A, B]] = _ compare _
9594

9695
implicit def catsDataAlignForConst[A: Semigroup]: Align[Const[A, *]] =
9796
new Align[Const[A, *]] {
@@ -192,8 +191,7 @@ sealed abstract private[data] class ConstInstances2 extends ConstInstances3 {
192191
implicit def catsDataSemigroupForConst[A: Semigroup, B]: Semigroup[Const[A, B]] =
193192
Semigroup.instance(_ combine _)
194193

195-
implicit def catsDataPartialOrderForConst[A: PartialOrder, B]: PartialOrder[Const[A, B]] =
196-
PartialOrder.from(_ partialCompare _)
194+
implicit def catsDataPartialOrderForConst[A: PartialOrder, B]: PartialOrder[Const[A, B]] = _ partialCompare _
197195

198196
implicit def catsDataApplicativeForConst[C](implicit C: Monoid[C]): Applicative[Const[C, *]] =
199197
new ConstApplicative[C] { val C0: Monoid[C] = C }

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

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

860-
implicit def catsDataOrderForIor[A: Order, B: Order]: Order[A Ior B] =
861-
Order.from(_ compare _)
860+
implicit def catsDataOrderForIor[A: Order, B: Order]: Order[A Ior B] = _ compare _
862861

863862
implicit def catsDataShowForIor[A: Show, B: Show]: Show[A Ior B] = _.show
864863

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +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-
Order.from(_ compare _)
406+
_ compare _
407407
}
408408

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

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ trait OrderInstances extends kernel.instances.OrderInstances {
4242
def contramap[A, B](fa: Order[A])(f: B => A): Order[B] =
4343
Order.by(f)(fa)
4444

45-
def product[A, B](fa: Order[A], fb: Order[B]): Order[(A, B)] =
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
49-
}
45+
def product[A, B](fa: Order[A], fb: Order[B]): Order[(A, B)] = { (x, y) =>
46+
val z = fa.compare(x._1, y._1)
47+
if (z == 0) fb.compare(x._2, y._2) else z
48+
}
5049
}
5150
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ trait PartialOrderInstances extends kernel.instances.PartialOrderInstances {
3434
*/
3535
def contramap[A, B](fa: PartialOrder[A])(f: B => A): PartialOrder[B] = PartialOrder.by[B, A](f)(fa)
3636

37-
def product[A, B](fa: PartialOrder[A], fb: PartialOrder[B]): PartialOrder[(A, B)] =
38-
PartialOrder.from { (x, y) =>
39-
val z = fa.partialCompare(x._1, y._1)
40-
if (z == 0.0) fb.partialCompare(x._2, y._2) else z
41-
}
37+
def product[A, B](fa: PartialOrder[A], fb: PartialOrder[B]): PartialOrder[(A, B)] = { (x, y) =>
38+
val z = fa.partialCompare(x._1, y._1)
39+
if (z == 0.0) fb.partialCompare(x._2, y._2) else z
40+
}
4241

4342
def unit: PartialOrder[Unit] = Order[Unit]
4443
}

‎kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,11 @@ class Tests extends TestsConfig with DisciplineSuite {
353353
// Comparison related
354354

355355
// Something that can give NaN for test
356-
def subsetPartialOrder[A]: PartialOrder[Set[A]] =
357-
PartialOrder.from { (x, y) =>
358-
if (x == y) 0.0
359-
else if (x.subsetOf(y)) -1.0
360-
else if (y.subsetOf(x)) 1.0
361-
else Double.NaN
362-
}
356+
def subsetPartialOrder[A]: PartialOrder[Set[A]] = (x, y) =>
357+
if (x == y) 0.0
358+
else if (x.subsetOf(y)) -1.0
359+
else if (y.subsetOf(x)) 1.0
360+
else Double.NaN
363361

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

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ object Order extends OrderFunctions[Order] with OrderToOrderingConversion {
156156
* function `f`.
157157
*/
158158
def by[@sp A, @sp B](f: A => B)(implicit ev: Order[B]): Order[A] =
159-
from((x, y) => ev.compare(f(x), f(y)))
159+
(x, y) => ev.compare(f(x), f(y))
160160

161161
/**
162162
* Defines an ordering on `A` from the given order such that all arrows switch direction.
163163
*/
164164
def reverse[@sp A](order: Order[A]): Order[A] =
165-
from((x, y) => order.compare(y, x))
165+
(x, y) => order.compare(y, x)
166166

167167
/**
168168
* Returns a new `Order[A]` instance that first compares by the first
@@ -171,18 +171,16 @@ object Order extends OrderFunctions[Order] with OrderToOrderingConversion {
171171
* That is, `Order.whenEqual(x, y)` creates an `Order` that first orders by `x` and
172172
* then (if two elements are equal) falls back to `y` for the comparison.
173173
*/
174-
def whenEqual[@sp A](first: Order[A], second: Order[A]): Order[A] =
175-
from { (x, y) =>
176-
val c = first.compare(x, y)
177-
if (c == 0) second.compare(x, y)
178-
else c
179-
}
174+
def whenEqual[@sp A](first: Order[A], second: Order[A]): Order[A] = { (x, y) =>
175+
val c = first.compare(x, y)
176+
if (c == 0) second.compare(x, y)
177+
else c
178+
}
180179

181180
/**
182181
* Define an `Order[A]` using the given function `f`.
183182
*/
184-
def from[@sp A](f: (A, A) => Int): Order[A] =
185-
f(_, _)
183+
def from[@sp A](f: (A, A) => Int): Order[A] = f(_, _)
186184

187185
/**
188186
* Define an `Order[A]` using the given 'less than' function `f`.
@@ -205,7 +203,7 @@ object Order extends OrderFunctions[Order] with OrderToOrderingConversion {
205203
* An `Order` instance that considers all `A` instances to be equal.
206204
*/
207205
def allEqual[A]: Order[A] =
208-
from((_, _) => 0)
206+
(_, _) => 0
209207

210208
/**
211209
* A `Monoid[Order[A]]` can be generated for all `A` with the following
@@ -231,10 +229,8 @@ object Order extends OrderFunctions[Order] with OrderToOrderingConversion {
231229
def fromOrdering[A](implicit ev: Ordering[A]): Order[A] =
232230
new Order[A] {
233231
def compare(x: A, y: A): Int = ev.compare(x, y)
234-
235232
override def toOrdering: Ordering[A] = ev
236233
}
237234

238-
def fromComparable[A <: Comparable[A]]: Order[A] =
239-
from(_ compareTo _)
235+
def fromComparable[A <: Comparable[A]]: Order[A] = _ compareTo _
240236
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,21 @@ object PartialOrder extends PartialOrderFunctions[PartialOrder] with PartialOrde
162162
* function `f`.
163163
*/
164164
def by[@sp A, @sp B](f: A => B)(implicit ev: PartialOrder[B]): PartialOrder[A] =
165-
from((x, y) => ev.partialCompare(f(x), f(y)))
165+
(x, y) => ev.partialCompare(f(x), f(y))
166166

167167
/**
168168
* Defines a partial order on `A` from p where all arrows switch direction.
169169
*/
170170
def reverse[@sp A](p: PartialOrder[A]): PartialOrder[A] =
171-
from((x, y) => p.partialCompare(y, x))
171+
(x, y) => p.partialCompare(y, x)
172172

173173
/**
174174
* Define a `PartialOrder[A]` using the given function `f`.
175175
*/
176-
def from[@sp A](f: (A, A) => Double): PartialOrder[A] =
177-
f(_, _)
176+
def from[@sp A](f: (A, A) => Double): PartialOrder[A] = f(_, _)
178177

179178
def fromPartialOrdering[A](implicit ev: PartialOrdering[A]): PartialOrder[A] =
180-
from((x, y) => ev.tryCompare(x, y).fold(Double.NaN)(_.toDouble))
179+
ev.tryCompare(_, _).fold(Double.NaN)(_.toDouble)
181180
}
182181

183182
trait PartialOrderToPartialOrderingConversion {

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ trait Semilattice[@sp(Int, Long, Float, Double) A] extends Any with Band[A] with
4141
* 1.0 if y = combine(x, y)
4242
* NaN otherwise
4343
*/
44-
def asMeetPartialOrder(implicit ev: Eq[A]): PartialOrder[A] =
45-
PartialOrder.from { (x, y) =>
46-
if (ev.eqv(x, y)) 0.0
47-
else {
48-
val z = self.combine(x, y)
49-
if (ev.eqv(x, z)) -1.0 else if (ev.eqv(y, z)) 1.0 else Double.NaN
50-
}
44+
def asMeetPartialOrder(implicit ev: Eq[A]): PartialOrder[A] = (x, y) =>
45+
if (ev.eqv(x, y)) 0.0
46+
else {
47+
val z = self.combine(x, y)
48+
if (ev.eqv(x, z)) -1.0 else if (ev.eqv(y, z)) 1.0 else Double.NaN
5149
}
5250

5351
/**
@@ -62,13 +60,11 @@ trait Semilattice[@sp(Int, Long, Float, Double) A] extends Any with Band[A] with
6260
* 1.0 if x = combine(x, y)
6361
* NaN otherwise
6462
*/
65-
def asJoinPartialOrder(implicit ev: Eq[A]): PartialOrder[A] =
66-
PartialOrder.from { (x, y) =>
67-
if (ev.eqv(x, y)) 0.0
68-
else {
69-
val z = self.combine(x, y)
70-
if (ev.eqv(y, z)) -1.0 else if (ev.eqv(x, z)) 1.0 else Double.NaN
71-
}
63+
def asJoinPartialOrder(implicit ev: Eq[A]): PartialOrder[A] = (x, y) =>
64+
if (ev.eqv(x, y)) 0.0
65+
else {
66+
val z = self.combine(x, y)
67+
if (ev.eqv(y, z)) -1.0 else if (ev.eqv(x, z)) 1.0 else Double.NaN
7268
}
7369
}
7470

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@ 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-
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-
}
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+
}
4142
}
4243

4344
implicit def catsDataMonoidForEither[A, B](implicit B: Monoid[B]): Monoid[Either[A, B]] =
@@ -74,19 +75,20 @@ private[instances] trait EitherInstances0 extends EitherInstances1 {
7475
A: PartialOrder[A],
7576
B: PartialOrder[B]
7677
): PartialOrder[Either[A, B]] =
77-
PartialOrder.from { (x, y) =>
78-
x match {
79-
case Left(xx) =>
80-
y match {
81-
case Left(yy) => A.partialCompare(xx, yy)
82-
case Right(_) => -1.0
83-
}
84-
case Right(xx) =>
85-
y match {
86-
case Left(_) => 1.0
87-
case Right(yy) => B.partialCompare(xx, yy)
88-
}
89-
}
78+
new PartialOrder[Either[A, B]] {
79+
def partialCompare(x: Either[A, B], y: Either[A, B]): Double =
80+
x match {
81+
case Left(xx) =>
82+
y match {
83+
case Left(yy) => A.partialCompare(xx, yy)
84+
case Right(_) => -1.0
85+
}
86+
case Right(xx) =>
87+
y match {
88+
case Left(_) => 1.0
89+
case Right(yy) => B.partialCompare(xx, yy)
90+
}
91+
}
9092
}
9193

9294
implicit def catsStdHashForEither[A, B](implicit A: Hash[A], B: Hash[B]): Hash[Either[A, B]] = new EitherHash[A, B]

0 commit comments

Comments
 (0)