Skip to content

Instantly share code, notes, and snippets.

@slouc
Last active May 3, 2019 09:53
Show Gist options
  • Save slouc/65e2e22d8f94e2d165121e36e18545a5 to your computer and use it in GitHub Desktop.
Save slouc/65e2e22d8f94e2d165121e36e18545a5 to your computer and use it in GitHub Desktop.
import cats._
abstract class Yoneda[F[_]: Functor, A] { self =>
def apply[B](f: A => B): F[B]
def run: F[A] = apply(identity)
def map[B](f: A => B): Yoneda[F, B] = new Yoneda[F, B] {
def apply[C](g: B => C): F[C] = self.apply(f andThen g)
}
}
def toYoneda[F[_]: Functor, A](fa: F[A]): Yoneda[F, A] =
new Yoneda[F, A] {
override def apply[B](f: (A) => B): F[B] = Functor[F].map(fa)(f)
}
def fromYoneda[F[_], A](y: Yoneda[F, A]): F[A] = y.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment