This is an old revision of the document!

6 - Monads

Monads, baby!
Today we look at certain kinds of computations from a truly functional perspective:

  • computations that might or might not have a value
  • computations that might fail
  • computations that will finish at some point

But we won't get into math and category theory for this.
We'll see what a monad is from a programming perspective, directly in the Scala language.

You can find today's contents here.


Let's implement some of our own monads.

  1. Let's write a monad equivalent with Option. Let's call this Maybe, as in Haskell.
    • create a trait Maybe[T] (remember the [T] is equivalent to Java's <T> syntax for generics)
    • add some functional primitives as methods to Maybe:
      • the monad requirements: map, flatMap, filter
      • zipWith, which receives a Maybe[A] and returns a Maybe[(T,A)]
        The signature is just a tiny bit more complex:
        def zipWith[A](other: Maybe[A]): Maybe[T,A]
sesiuni/scala/lab6.1467637025.txt.gz · Last modified: 2016/07/04 15:57 by dciocirlan