From 02c2de4706315d1d9d1ac3763e9605dc987c381a Mon Sep 17 00:00:00 2001 From: Georgi Krastev Date: Fri, 21 May 2021 17:03:22 +0300 Subject: [PATCH] Cleanup Const - it's provided by shapeless Also use the constant type directly for brevity --- .../src/main/scala-3/cats/derived/emptyk.scala | 4 ++-- .../main/scala-3/cats/derived/foldable.scala | 7 +++---- .../main/scala-3/cats/derived/functor.scala | 11 ++++------- .../main/scala-3/cats/derived/monoidk.scala | 8 +++----- .../main/scala-3/cats/derived/package.scala | 4 ---- .../main/scala-3/cats/derived/semigroupk.scala | 4 ++-- .../main/scala-3/cats/derived/traverse.scala | 18 +++++++----------- 7 files changed, 21 insertions(+), 35 deletions(-) delete mode 100644 core/src/main/scala-3/cats/derived/package.scala diff --git a/core/src/main/scala-3/cats/derived/emptyk.scala b/core/src/main/scala-3/cats/derived/emptyk.scala index 395cc167..ca79945f 100644 --- a/core/src/main/scala-3/cats/derived/emptyk.scala +++ b/core/src/main/scala-3/cats/derived/emptyk.scala @@ -1,7 +1,7 @@ package cats.derived import alleycats.{Empty, EmptyK} -import shapeless3.deriving.K1 +import shapeless3.deriving.{Const, K1} object emptyk extends EmptyKDerivation @@ -16,4 +16,4 @@ trait EmptyKDerivation: new ProductEmptyK[EmptyK, F]{} given [X](using X: Empty[X]): EmptyK[Const[X]] with - def empty[A]: Const[X][A] = X.empty + def empty[A]: X = X.empty diff --git a/core/src/main/scala-3/cats/derived/foldable.scala b/core/src/main/scala-3/cats/derived/foldable.scala index 6314a894..48539f70 100644 --- a/core/src/main/scala-3/cats/derived/foldable.scala +++ b/core/src/main/scala-3/cats/derived/foldable.scala @@ -1,7 +1,7 @@ package cats.derived import cats.{Eval, Foldable} -import shapeless3.deriving.{K1, Continue} +import shapeless3.deriving.{Const, Continue, K1} object foldable extends FoldableDerivation @@ -43,6 +43,5 @@ trait FoldableDerivation: new CoproductFoldable[Foldable, F]{} given [X]: Foldable[Const[X]] with - def foldLeft[A, B](fa: Const[X][A], b: B)(f: (B, A) => B): B = b - - def foldRight[A, B](fa: Const[X][A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = lb + def foldLeft[A, B](fa: X, b: B)(f: (B, A) => B): B = b + def foldRight[A, B](fa: X, lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = lb diff --git a/core/src/main/scala-3/cats/derived/functor.scala b/core/src/main/scala-3/cats/derived/functor.scala index 5c63b44b..bf24ae6b 100644 --- a/core/src/main/scala-3/cats/derived/functor.scala +++ b/core/src/main/scala-3/cats/derived/functor.scala @@ -1,11 +1,11 @@ package cats.derived import cats.Functor -import shapeless3.deriving.K1 +import shapeless3.deriving.{Const, K1} object functor extends FunctorDerivation -trait ProductFunctor[T[x[_]] <: Functor[x], F[_]](using inst: K1.Instances[T, F]) +trait GenericFunctor[T[x[_]] <: Functor[x], F[_]](using inst: K1.Instances[T, F]) extends Functor[F]: def map[A, B](fa: F[A])(f: A => B): F[B] = @@ -16,10 +16,7 @@ trait ProductFunctor[T[x[_]] <: Functor[x], F[_]](using inst: K1.Instances[T, F] trait FunctorDerivation: extension (F: Functor.type) inline def derived[F[_]](using gen: K1.Generic[F]): Functor[F] = - new ProductFunctor[Functor, F]{} - - given functorGen[F[_]](using inst: => K1.ProductInstances[Functor, F]): Functor[F] = - new ProductFunctor[Functor, F]{} + new GenericFunctor[Functor, F]{} given [X]: Functor[Const[X]] with - def map[A, B](fa: Const[X][A])(f: A => B): Const[X][B] = fa + def map[A, B](fa: X)(f: A => B): X = fa diff --git a/core/src/main/scala-3/cats/derived/monoidk.scala b/core/src/main/scala-3/cats/derived/monoidk.scala index aa1ebc53..30c11d8f 100644 --- a/core/src/main/scala-3/cats/derived/monoidk.scala +++ b/core/src/main/scala-3/cats/derived/monoidk.scala @@ -1,7 +1,7 @@ package cats.derived import cats.{Monoid, MonoidK} -import shapeless3.deriving.K1 +import shapeless3.deriving.{Const, K1} object monoidk extends MonoidKDerivation @@ -14,7 +14,5 @@ trait MonoidKDerivation: inline def derived[F[_]](using gen: K1.Generic[F]): MonoidK[F] = ??? given [X](using X: Monoid[X]): MonoidK[Const[X]] with - def empty[A]: Const[X][A] = X.empty - - def combineK[A](x: Const[X][A], y: Const[X][A]): Const[X][A] = - X.combine(x, y) + def empty[A]: X = X.empty + def combineK[A](x: X, y: X): X = X.combine(x, y) diff --git a/core/src/main/scala-3/cats/derived/package.scala b/core/src/main/scala-3/cats/derived/package.scala deleted file mode 100644 index eb7c4452..00000000 --- a/core/src/main/scala-3/cats/derived/package.scala +++ /dev/null @@ -1,4 +0,0 @@ -package cats.derived - -type Id = [t] =>> t -type Const[X] = [t] =>> X diff --git a/core/src/main/scala-3/cats/derived/semigroupk.scala b/core/src/main/scala-3/cats/derived/semigroupk.scala index 82248a7c..d486529b 100644 --- a/core/src/main/scala-3/cats/derived/semigroupk.scala +++ b/core/src/main/scala-3/cats/derived/semigroupk.scala @@ -1,7 +1,7 @@ package cats.derived import cats.{Semigroup, SemigroupK} -import shapeless3.deriving.K1 +import shapeless3.deriving.{Const, K1} object semigroupk extends SemigroupKDerivation @@ -17,5 +17,5 @@ trait SemigroupKDerivation: new ProductSemigroupK[SemigroupK, F]{} given [X](using X: Semigroup[X]): SemigroupK[Const[X]] with - def combineK[A](x: Const[X][A], y: Const[X][A]): Const[X][A] = + def combineK[A](x: X, y: X): X = X.combine(x, y) diff --git a/core/src/main/scala-3/cats/derived/traverse.scala b/core/src/main/scala-3/cats/derived/traverse.scala index 10799cae..34afc5d2 100644 --- a/core/src/main/scala-3/cats/derived/traverse.scala +++ b/core/src/main/scala-3/cats/derived/traverse.scala @@ -1,12 +1,12 @@ package cats.derived import cats.{Applicative, Eval, Traverse} -import shapeless3.deriving.{K1, Continue} +import shapeless3.deriving.{Const, Continue, K1} object traverse extends TraverseDerivation trait ProductTraverse[T[x[_]] <: Traverse[x], F[_]](using inst: K1.ProductInstances[T, F]) - extends ProductFunctor[T, F], ProductFoldable[T, F], Traverse[F]: + extends GenericFunctor[T, F], ProductFoldable[T, F], Traverse[F]: def traverse[G[_], A, B](fa: F[A])(f: A => G[B])(using G: Applicative[G]): G[F[B]] = inst.traverse[A, G, B](fa)([a,b] => (ga: G[a], f: a => b) => G.map(ga)(f))([a] => (x: a) => G.pure(x))([a,b] => (gf: G[a => b], ga: G[a]) => G.ap(gf)(ga))( @@ -14,7 +14,7 @@ trait ProductTraverse[T[x[_]] <: Traverse[x], F[_]](using inst: K1.ProductInstan ) trait CoproductTraverse[T[x[_]] <: Traverse[x], F[_]](using inst: K1.CoproductInstances[T, F]) - extends ProductFunctor[T, F], CoproductFoldable[T, F], Traverse[F]: + extends GenericFunctor[T, F], CoproductFoldable[T, F], Traverse[F]: def traverse[G[_], A, B](fa: F[A])(f: A => G[B])(using G: Applicative[G]): G[F[B]] = inst.traverse[A, G, B](fa)([a,b] => (ga: G[a], f: a => b) => G.map(ga)(f))([a] => (x: a) => G.pure(x))([a,b] => (gf: G[a => b], ga: G[a]) => G.ap(gf)(ga))( @@ -33,11 +33,7 @@ trait TraverseDerivation: new CoproductTraverse[Traverse, F]{} given [X]: Traverse[Const[X]] with - override def map[A, B](fa: Const[X][A])(f: A => B): Const[X][B] = fa - - def foldLeft[A, B](fa: Const[X][A], b: B)(f: (B, A) => B): B = b - - def foldRight[A, B](fa: Const[X][A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = lb - - def traverse[G[_]: Applicative, A, B](fa: Const[X][A])(f: A => G[B]): G[Const[X][B]] = - Applicative[G].pure(fa) + override def map[A, B](fa: X)(f: A => B): X = fa + def foldLeft[A, B](fa: X, b: B)(f: (B, A) => B): B = b + def foldRight[A, B](fa: X, lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = lb + def traverse[G[_]: Applicative, A, B](fa: X)(f: A => G[B]): G[X] = Applicative[G].pure(fa)