Transform typeclass
Nobody has claimed this yet.
- Dominant language
- Scala
- Stars
- 5.5k
- Forks
- 1.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 5
Description
There has been some discussion about adding a typeclass for dealing with F ~> G transformations in #1644. The original proposal was made by @kailuowang in this gist and I materialized it by the name of Transform. The example below includes only instances for EitherT and Kleisli.
import cats.arrow.FunctionK
import cats.data.{EitherT, Kleisli}
import cats.~>
trait Transform[H[_[_], _]] {
def map[F[_], G[_], A](ha: H[F, A])(f: F ~> G): H[G, A]
def lift[F[_], G[_]](f: F ~> G): H[F, ?] ~> H[G, ?] =
new FunctionK[H[F, ?], H[G, ?]] {
def apply[A](fa: H[F, A]): H[G, A] = map(fa)(f)
}
}
object Transform {
implicit def eitherTTransform[E]: Transform[EitherT[?[_], E, ?]] =
new Transform[EitherT[?[_], E, ?]] {
def map[F[_], G[_], A](ha: EitherT[F, E, A])(f: F ~> G): EitherT[G, E, A] =
EitherT[G, E, A](f(ha.value)) //This should be EitherT's transformF
}
implicit def kleisliTransform[E]: Transform[Kleisli[?[_], E, ?]] =
new Transform[Kleisli[?[_], E, ?]] {
def map[F[_], G[_], A](ha: Kleisli[F, E, A])(f: F ~> G): Kleisli[G, E, A] =
ha.transform(f)
}
}
The pattern is apparent in Kleisli since it already implements transform. For the rest of the monad transformers it is not defined as a F ~> G and it is confusing given that for EitherT and OptionT transform means to bimap/map the inner types. This might be an opportunity to make standard the F ~> G operations in MonadTs and solve the confusion for the actual use of transform.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Review the proposed Transform typeclass, cats.arrow.FunctionK, and the existing EitherT and Kleisli APIs. Compare how the other monad transformers expose F ~> G operations and clarify the intended meaning of transform. Done means an agreed, consistent design for the typeclass and its MonadT instances, with coverage for the affected operations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100