Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. import cats.arrow.FunctionK
  2. import cats.{Functor, ~>}
  3. import polymorphic.Exists
  4. import cats.syntax.functor._
  5. import cats.instances.list._
  6. import polymorphic.Exists._
  7.  
  8. case class CoyonedaF[F[_], A, E](fe: F[E], ff: E => A) {
  9. def foldMap[G[_] : Functor](fK: F ~> G): G[A] =
  10. fK(fe).map(ff)
  11. }
  12.  
  13. type Coyoneda[F[_], A] = Exists[CoyonedaF[F, A, *]]
  14. object Coyoneda {
  15. def apply[F[_], A, E](fe: F[E], f: E => A): Coyoneda[F, A] =
  16. Exists(CoyonedaF(fe, f))
  17.  
  18. def lift[F[_], A](fa: F[A]): Coyoneda[F, A] = apply(fa, identity[A])
  19.  
  20. implicit def functor[F[_]]: Functor[Coyoneda[F, *]] = new Functor[Coyoneda[F, *]] {
  21. def map[A, B](fa: Coyoneda[F, A])(f: A => B): Coyoneda[F, B] = {
  22. val ffa = unwrap(fa)
  23. Coyoneda(ffa.fe, f compose ffa.ff)
  24. }
  25. }
  26. }
  27.  
  28. object CoyonedaExample extends App {
  29. import Coyoneda._
  30.  
  31. val res = unwrap(Coyoneda.lift(List(1,2,3)).map(_ + 10)).foldMap(FunctionK.id)
  32.  
  33. println(res)
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement