Skip to content

Instantly share code, notes, and snippets.

@jdegoes
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdegoes/9898272 to your computer and use it in GitHub Desktop.
Save jdegoes/9898272 to your computer and use it in GitHub Desktop.
A default ApplicativePlus for Applicatives that contain ApplicativePlus's.
implicit def LiftedApplicativePlus[F[_], G[_]]
(implicit F: Applicative[F], G: ApplicativePlus[G]) = new ApplicativePlus[({type f[A]=F[G[A]]})#f] {
def empty[A] = F.point(G.empty)
def point[A](a: => A): F[G[A]] = F.point(G.point(a))
def plus[A](v1: F[G[A]], v2: => F[G[A]]): F[G[A]] = {
F.apply2(v1, v2)(G.plus(_, _))
}
def ap[A, B](fa: => F[G[A]])(f: => F[G[A => B]]): F[G[B]] = {
F.ap(fa)(F.map(f)(f => x => G.apply2(f, x)((f, x) => f(x))))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment