Created
April 7, 2020 18:45
-
-
Save polyglotpiglet/56f32dc9de84c578e3232b46cd98d40c to your computer and use it in GitHub Desktop.
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
/* | |
3 Monad laws | |
1. Right identity: pure(a).flatMap(func) == func(a) | |
2. m.flatMap(pure) = m | |
3. m.flatMap(f).flatMap(g) = m.flatMap(x => f(x).flatMap(g)) | |
*/ | |
trait MyMonad[F[_]] { | |
def pure[A](a: A): F[A] | |
def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B] | |
// all monads are functors because we can define map in terms of pure and flatMap | |
def map[A, B](fa: F[A], f: A => B): F[B] = flatMap(fa)(a => pure(f(a))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment