Skip to content

Instantly share code, notes, and snippets.

@ziwon
Last active December 29, 2015 09:49
Show Gist options
  • Save ziwon/7652757 to your computer and use it in GitHub Desktop.
Save ziwon/7652757 to your computer and use it in GitHub Desktop.
Functor, Applicative Functor and Monad
// A => B
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
// F[A => B]
trait Applicative[F[_]] extends Functor[F] {
def pure[A](a: A): F[A]
def apply[A, B](f: F[A => B]): F[A] => F[B]
}
// A => F[B]
trait Monad[F[_]] extends Applicative[F] {
def flatMap[A, B](f: A => F[B]): F[A] => F[B]
}
object Applicative {
// to implement
// ...
}
/*
Your test codes
val add = (a: Int) => (b: Int) => (c: Int) => a + b + c
val none: Option[Int] = None
println(Some(7) <*> (Some(8) <*> (Some(9) > add)))
println(Some(7) <*> (none <*> (Some(9) > add)))
println(List(1, 2, 3) <*> (List(77) <*> (List(9, 13) > add)))
*/