Pagini
Workshops
Parteneri
This is an old revision of the document!
Monads, baby!
Today we look at certain kinds of computations from a truly functional perspective:
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.
Option
. Let's call this Maybe
, as in Haskell.Maybe[T]
(remember the [T]
is equivalent to Java's <T>
syntax for generics)Maybe
:map
, flatMap
, filter
zipWith
, which receives a Maybe[A]
and returns a Maybe[(T,A)]
def zipWith[A](other: Maybe[A]): Maybe[(T,A)]
exists
, which receives a predicate (a function of T to Boolean) and returns true if this instance contains an element which satisfies the predicateMaybe
trait:isEmpty
toList
size
Just[T]
and None
and implement the methods