Skip to content

Instantly share code, notes, and snippets.

@polyglotpiglet
Created April 7, 2020 18:45
Show Gist options
  • Save polyglotpiglet/56f32dc9de84c578e3232b46cd98d40c to your computer and use it in GitHub Desktop.
Save polyglotpiglet/56f32dc9de84c578e3232b46cd98d40c to your computer and use it in GitHub Desktop.
/*
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