Skip to content

Commit

Permalink
Cleanup Const - it's provided by shapeless
Browse files Browse the repository at this point in the history
Also use the constant type directly for brevity
  • Loading branch information
joroKr21 committed May 21, 2021
1 parent 7ff0189 commit a2a9dd6
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 34 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala-3/cats/derived/emptyk.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats.derived

import alleycats.{Empty, EmptyK}
import shapeless3.deriving.K1
import shapeless3.deriving.{Const, K1}

object emptyk extends EmptyKDerivation

Expand Down
7 changes: 3 additions & 4 deletions core/src/main/scala-3/cats/derived/foldable.scala
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
11 changes: 4 additions & 7 deletions core/src/main/scala-3/cats/derived/functor.scala
Original file line number Diff line number Diff line change
@@ -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] =
Expand All @@ -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
8 changes: 3 additions & 5 deletions core/src/main/scala-3/cats/derived/monoidk.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats.derived

import cats.{Monoid, MonoidK}
import shapeless3.deriving.K1
import shapeless3.deriving.{Const, K1}

object monoidk extends MonoidKDerivation

Expand All @@ -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)
4 changes: 0 additions & 4 deletions core/src/main/scala-3/cats/derived/package.scala

This file was deleted.

4 changes: 2 additions & 2 deletions core/src/main/scala-3/cats/derived/semigroupk.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats.derived

import cats.{Semigroup, SemigroupK}
import shapeless3.deriving.K1
import shapeless3.deriving.{Const, K1}

object semigroupk extends SemigroupKDerivation

Expand All @@ -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)
18 changes: 7 additions & 11 deletions core/src/main/scala-3/cats/derived/traverse.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
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))(
[t[_]] => (trav: T[t], t0: t[A]) => trav.traverse[G, A, B](t0)(f)
)

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))(
Expand All @@ -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)

0 comments on commit a2a9dd6

Please sign in to comment.