Instantly share code, notes, and snippets.

What would you like to do?
Monads in Scala
trait M[T] {
def flatMap[U](f: T => M[U]): M[U]
def unit[T](x: T): M[T]
val l = List(1) => x * 2)
l.flatMap(x => List(x * 2))
// left unit
def f(x:Int): List[Int] = List(x * 2)
l flatMap f
// right unit
// associativity
def g(x:Int): List[Int] = List(x * 3)
l flatMap f flatMap g
l flatMap f flatMap(x => g(x))
l flatMap(x => f(x)) flatMap g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment