-
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.
* Derive Group on Scala 3 * Add a spec for DerivedGroup
- Loading branch information
Showing
5 changed files
with
118 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cats.derived | ||
|
||
import cats.Group | ||
import shapeless3.deriving.K0.* | ||
|
||
import scala.annotation.* | ||
import scala.compiletime.* | ||
|
||
@implicitNotFound("""Could not derive an instance of Group[A] where A = ${A}. | ||
Make sure that A is a case class where all fields have a Group instance.""") | ||
type DerivedGroup[A] = Derived[Group[A]] | ||
object DerivedGroup: | ||
type Or[A] = Derived.Or[Group[A]] | ||
|
||
@nowarn("msg=unused import") | ||
inline def apply[A]: Group[A] = | ||
import DerivedGroup.given | ||
summonInline[DerivedGroup[A]].instance | ||
|
||
@nowarn("msg=unused import") | ||
inline def strict[A]: Group[A] = | ||
import Strict.given | ||
summonInline[DerivedGroup[A]].instance | ||
|
||
given [A](using inst: => ProductInstances[Or, A]): DerivedGroup[A] = | ||
Strict.product(using inst.unify) | ||
|
||
trait Product[F[x] <: Group[x], A](using inst: ProductInstances[F, A]) extends DerivedMonoid.Product[F, A], Group[A]: | ||
override def inverse(a: A): A = inst.map(a)([a] => (F: F[a], x: a) => F.inverse(x)) | ||
|
||
object Strict: | ||
given product[A: ProductInstancesOf[Group]]: DerivedGroup[A] = | ||
new Product[Group, A] {} |
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
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,74 @@ | ||
/* | ||
* Copyright (c) 2016 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.Group | ||
import cats.kernel.laws.discipline.{GroupTests, SerializableTests} | ||
|
||
import scala.compiletime.* | ||
import scala.concurrent.duration.Duration | ||
|
||
class GroupSuite extends KittensSuite: | ||
import GroupSuite.* | ||
|
||
inline def tests[A]: GroupTests[A] = | ||
GroupTests[A](using summonInline) | ||
|
||
inline def validate(inline instance: String): Unit = | ||
checkAll(s"$instance[Slice]", tests[Slice].group) | ||
checkAll(s"$instance[Compared]", tests[Compared].group) | ||
checkAll(s"$instance is Serializable", SerializableTests.serializable(summonInline[Group[Slice]])) | ||
|
||
locally: | ||
import auto.group.given | ||
validate("auto.group") | ||
|
||
locally: | ||
import semiInstances.given | ||
validate("semiauto.group") | ||
|
||
locally: | ||
import strictInstances.given | ||
validate("strict.semiauto.group") | ||
testNoInstance("strict.semiauto.group", "Top") | ||
|
||
locally: | ||
import derivedInstances.* | ||
val instance = "derived.group" | ||
checkAll(s"$instance[Slice]", tests[Slice].group) | ||
checkAll(s"$instance[Compared]", tests[Compared].group) | ||
checkAll(s"$instance is Serializable", SerializableTests.serializable(Group[Slice])) | ||
|
||
end GroupSuite | ||
|
||
object GroupSuite: | ||
case class Slice(count: Long, percentile: Double, duration: Duration) | ||
case class Compared(x: Slice, y: Slice) | ||
|
||
object semiInstances: | ||
given Group[Slice] = semiauto.group | ||
given Group[Compared] = semiauto.group | ||
|
||
object strictInstances: | ||
given Group[Slice] = strict.semiauto.group | ||
given Group[Compared] = strict.semiauto.group | ||
|
||
object derivedInstances: | ||
case class Slice(x: GroupSuite.Slice) derives Group | ||
case class Compared(x: GroupSuite.Compared) derives Group | ||
|
||
end GroupSuite |
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