Last active
December 29, 2015 09:49
-
-
Save ziwon/7652757 to your computer and use it in GitHub Desktop.
Functor, Applicative Functor and Monad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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))) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Functors: http://tmorris.net/posts/functors-and-things-using-scala/index.html
Applicative Functors: http://tmorris.net/posts/applicative-functors-in-scala/index.html
Applicatives are generalized functors: http://hseeberger.wordpress.com/2011/01/31/applicatives-are-generalized-functors/
Functors, Applicatives, And Monads In Pictures (한글)
https://github.com/netpyoung/netpyoung.Notes/tree/master/external/haskell/functors_applicatives_and_monads_in_pictures