-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Port Order to Dotty * MR comments fixes
- Loading branch information
1 parent
5896436
commit 56d551d
Showing
6 changed files
with
111 additions
and
37 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,43 @@ | ||
package cats.derived | ||
|
||
import cats.{Order, Show} | ||
|
||
import scala.compiletime.* | ||
import shapeless3.deriving.{Complete, Continue, K0, Labelling} | ||
|
||
import scala.annotation.targetName | ||
import scala.deriving.Mirror | ||
|
||
type DerivedOrder[A] = Derived[Order[A]] | ||
object DerivedOrder: | ||
type Or[A] = Derived.Or[Order[A]] | ||
|
||
inline def apply[A]: Order[A] = | ||
import DerivedOrder.given | ||
summonInline[DerivedOrder[A]].instance | ||
|
||
given product[A](using inst: => K0.ProductInstances[Or, A]): DerivedOrder[A] = | ||
given K0.ProductInstances[Order, A] = inst.unify | ||
new Product[Order, A] {} | ||
|
||
given coproduct[A](using inst: => K0.CoproductInstances[Or, A]): DerivedOrder[A] = | ||
given K0.CoproductInstances[Order, A] = inst.unify | ||
new Coproduct[Order, A] {} | ||
|
||
trait Product[T[x] <: Order[x], A](using inst: K0.ProductInstances[T, A]) extends Order[A]: | ||
|
||
def compare(x: A, y: A): Int = | ||
inst.foldLeft2(x, y)(0: Int)( | ||
[t] => | ||
(acc: Int, ord: T[t], t0: t, t1: t) => { | ||
val cmp = ord.compare(t0, t1) | ||
Complete(cmp != 0)(cmp)(acc) | ||
} | ||
) | ||
|
||
trait Coproduct[T[x] <: Order[x], A](using inst: K0.CoproductInstances[T, A]) extends Order[A]: | ||
|
||
def compare(x: A, y: A): Int = | ||
inst.fold2(x, y)((x: Int, y: Int) => x - y)( | ||
[t] => (ord: T[t], t0: t, t1: t) => ord.compare(t0, t1) | ||
) |
This file was deleted.
Oops, something went wrong.
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,61 @@ | ||
/* | ||
* Copyright (c) 2015 Miles Sabin | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.derived | ||
|
||
import cats.Order | ||
import cats.kernel.laws.discipline.{OrderTests, SerializableTests} | ||
import org.scalacheck.Arbitrary | ||
import scala.compiletime.summonInline | ||
|
||
class OrderSuite extends KittensSuite { | ||
import OrderSuite._ | ||
import TestDefns._ | ||
|
||
inline def orderTests[A]: OrderTests[A] = | ||
OrderTests[A](summonInline) | ||
|
||
inline def testOrder(context: String): Unit = { | ||
checkAll(s"$context.Order[Inner]", orderTests[Inner].order) | ||
checkAll(s"$context.Order[Outer]", orderTests[Outer].order) | ||
checkAll(s"$context.Order[Interleaved[Int]]", orderTests[Interleaved[Int]].order) | ||
checkAll(s"$context.Order[Recursive]", orderTests[Recursive].order) | ||
checkAll(s"$context.Order[GenericAdt[Int]]", orderTests[GenericAdt[Int]].order) | ||
checkAll(s"$context.Order is Serializable", SerializableTests.serializable(summonInline[Order[Interleaved[Int]]])) | ||
} | ||
|
||
{ | ||
import auto.order.given | ||
testOrder("auto") | ||
} | ||
|
||
{ | ||
import semiInstances.given | ||
testOrder("semiauto") | ||
} | ||
} | ||
|
||
object OrderSuite { | ||
import TestDefns._ | ||
|
||
object semiInstances { | ||
given Order[Inner] = semiauto.order | ||
given Order[Outer] = semiauto.order | ||
given Order[Interleaved[Int]] = semiauto.order | ||
given Order[Recursive] = semiauto.order | ||
given Order[GenericAdt[Int]] = semiauto.order | ||
} | ||
} |
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