Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cats.arrow.FunctionK
- import cats.{Functor, ~>}
- import polymorphic.Exists
- import cats.syntax.functor._
- import cats.instances.list._
- import polymorphic.Exists._
- case class CoyonedaF[F[_], A, E](fe: F[E], ff: E => A) {
- def foldMap[G[_] : Functor](fK: F ~> G): G[A] =
- fK(fe).map(ff)
- }
- type Coyoneda[F[_], A] = Exists[CoyonedaF[F, A, *]]
- object Coyoneda {
- def apply[F[_], A, E](fe: F[E], f: E => A): Coyoneda[F, A] =
- Exists(CoyonedaF(fe, f))
- def lift[F[_], A](fa: F[A]): Coyoneda[F, A] = apply(fa, identity[A])
- implicit def functor[F[_]]: Functor[Coyoneda[F, *]] = new Functor[Coyoneda[F, *]] {
- def map[A, B](fa: Coyoneda[F, A])(f: A => B): Coyoneda[F, B] = {
- val ffa = unwrap(fa)
- Coyoneda(ffa.fe, f compose ffa.ff)
- }
- }
- }
- object CoyonedaExample extends App {
- import Coyoneda._
- val res = unwrap(Coyoneda.lift(List(1,2,3)).map(_ + 10)).foldMap(FunctionK.id)
- println(res)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement