Skip to content

Instantly share code, notes, and snippets.

@gszeliga
Created February 18, 2015 22:46
Show Gist options
  • Save gszeliga/bfe6b60cdeee466b8cfc to your computer and use it in GitHub Desktop.
Save gszeliga/bfe6b60cdeee466b8cfc to your computer and use it in GitHub Desktop.
trait Functor[F[_]]{
def map[A,B](fa: F[A])(f: A => B): F[B]
}
trait Applicative[F[_]] extends Functor[F]
{
def map2[A,B,C](fa: F[A], fb: F[B])(f: (A,B) => C): F[C] = {
apply(map(fa)(a => f(a,_:B)))(fb)
}
def apply[A,B](a: F[A => B])(fa:F[A]):F[B] = {
map2(a,fa)((f,v) => f(v))
}
override def map[A,B](fa: F[A])(f: A => B):F[B] = {
map2(unit(f),fa)((f,v) => f(v))
}
def unit[A](a: A):F[A]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment