-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Composed instances from cats, invariant and contravariant
- Loading branch information
Showing
10 changed files
with
142 additions
and
8 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
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
20 changes: 20 additions & 0 deletions
20
dotty/perspective/src/main/scala/perspective/ContravariantK.scala
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,20 @@ | ||
package perspective | ||
|
||
import cats.Contravariant | ||
import cats.syntax.all.* | ||
|
||
trait ContravariantK[F[_[_], _]] extends InvariantK[F]: | ||
extension [A[_], C](fa: F[A, C]) | ||
/** A higher kinded equivalent of [[cats.Contravariant.contramap]]. */ | ||
def contramapK[B[_]](f: B :~>: A): F[B, C] | ||
|
||
override def imapK[B[_]](f: A :~>: B)(g: B :~>: A): F[B, C] = contramapK(g) | ||
|
||
object ContravariantK: | ||
given composeCats[F[_], G[_[_]]](using F: Contravariant[F], G: FunctorKC[G]): ContravariantKC[[H[_]] =>> F[G[H]]] with | ||
extension [A[_], C](fa: F[G[A]]) | ||
override def contramapK[B[_]](f: B :~>: A): F[G[B]] = fa.contramap(_.mapK(f)) | ||
|
||
given composeId[F[_], X](using F: Contravariant[F]): ContravariantKC[[H[_]] =>> F[H[X]]] = composeCats[F, IdFC[X]] | ||
|
||
type ContravariantKC[F[_[_]]] = ContravariantK[IgnoreC[F]] |
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
20 changes: 20 additions & 0 deletions
20
dotty/perspective/src/main/scala/perspective/InvariantK.scala
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,20 @@ | ||
package perspective | ||
|
||
import cats.Invariant | ||
import cats.syntax.all.* | ||
|
||
trait InvariantK[F[_[_], _]]: | ||
extension [A[_], C](fa: F[A, C]) | ||
/** A higher kinded equivalent of [[cats.Invariant.imap]]. */ | ||
def imapK[B[_]](f: A :~>: B)(g: B :~>: A): F[B, C] | ||
|
||
object InvariantK: | ||
given idInstanceC[A]: InvariantKC[IdFC[A]] = instances.idInstanceC[A] | ||
|
||
given composeCats[F[_], G[_[_]]](using F: Invariant[F], G: InvariantKC[G]): InvariantKC[[H[_]] =>> F[G[H]]] with | ||
extension [A[_], C](fa: F[G[A]]) | ||
override def imapK[B[_]](f: A :~>: B)(g: B :~>: A): F[G[B]] = fa.imap(_.imapK(f)(g))(_.imapK(g)(f)) | ||
|
||
given composeId[F[_], X](using F: Invariant[F]): InvariantKC[[H[_]] =>> F[H[X]]] = composeCats[F, IdFC[X]] | ||
|
||
type InvariantKC[F[_[_]]] = InvariantK[IgnoreC[F]] |
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