Continuum Logo Zwart

A gentle introduction to Monads

apr 15, 2021 | .data, .java, .script, AI, Development


What the hell are monads you ask?


The formal wikipedia definition says:

In functional programming, a monad is an abstraction that allows structuring programs generically. Supporting languages may use monads to abstract away boilerplate code needed by the program logic. Monads achieve this by providing their own data type (a particular type for each type of monad), which represents a specific form of computation, along with one procedure to wrap values of any basic type within the monad (yielding a monadic value) and another to compose functions that output monadic values (called monadic functions).


  1. person X works long and hard, and groks monads
  2. person X experiences amazing feeling of enlightenment, wonders why others are not similarly enlightened
  3. person X gives horrible, incomplete, inaccurate, oversimplified, and confusing explanation of monads to others which probably makes them think that monads are stupid, dumb, worthless, overcomplicated, unnecessary, something incorrect, or a mental wank-ercise
  • Operations that return a value or an error
  • Operations that perform operations on a List/Array
  • Operations that have an asynchronous nature.
  • Etc.
  • Execute the rest of the code only if there was no error
  • Execute the rest of the code once for each value in the list
  • Execute the rest of the code once the asynchronous code is finished
  • Etc.




  1. Give insights why it’s a powerful concept that is so much more than the implementations most object oriented programmers are familiar with (Java Optional, etc)




Almost all the code and the examples in this blogpost are written in Scala. Why Scala you ask?

  1. Scala is the preferred programming language for many data engineers.
  2. Scala can make senior developers feel very junior. Old programmers like me like to feel young again.
  3. Scala is sexy




Back to monads?


Let’s get started and do a conceptual dive into these mysterious monads.



  1. Yes, a Java Optional is a monad because it has a FlapMap function and it’s a wrapper concept.
def unit: A → F[A]
def map: F[A] → (A → B) → F[B]
  • We need a Flatten functionality that allows us to unpack a type A inside 2 boxes F.
def flatten: F[F[A]] → F[A]
  1. Our M type has a trait (interface in JAVA) that says that we need a FlapMap function on our wrapper type. That FlapMap function takes a function as parameter and that function maps a type A to a type B (wrapped inside our wrapper/monad type).

0*1ny8d uMT7ribgIi

unit(x).flatMap(f) == f(x)
m.flatMap(unit) == m
m.flatMap(f).flatMap(g) == m.flatMap(x ⇒ f(x).flatMap(g))
Let's dive into the world of monads laws ...
left identity law -> both below statement are equal
right identity law -> both below statement are equal
associativity law -> both below statement are equal
Let's do some sequencing ...
  1. Using the flatmap operator you can see that our flatmap function remains very clean.
I want to use them and combine them in useful logic. We are in luck. They have Option return types and that happens to be a Monad … This means that we have a flatmap function and this means that we can build sequences with these functions.
the monad way ...
  • if it returns a Some, the flatMap method calls our function and passes us the integer aNum;
  • the second call to parseInt returns a None or a Some;
  • if it returns a Some, the flatMap method calls our function and passes us bNum;
  • the call to divide returns a None or a Some, which is our result.
monads 5 1
  • A FlapMap function
  • An optional Map function
  1. We create our FlatMap function and remember that we will use this for sequencing a chain of operations. It’s identical to our map implementation but now we append the message instead of replacing it.

We seem to have a custom monad. Now let’s create some functions that use our monad (they are all pure functions that return our monad).

f: input: 100, result: 101
g: input: 101, result: 103
h: input: 103, result: 106
final Scala for comprehension value: 106
final Scala for comprehension msg:
f: input: 100, result: 101
g: input: 101, result: 103
h: input: 103, result: 106
Kristof Slechten

Kristof Slechten

Software Crafter

Kristof Slechten behaalde een master Informatica aan de VUB en is gespecialiseerd in projecten die betrekking hebben op big data & machine learning. Momenteel is Kristof aan de slag bij Imes Dexis waar hij onderzoekstrajecten rond machine learning uitwerkt. Daarnaast werkt Kristof mee aan verschillende interne onderzoeksprojecten rond AI.