Recently I’ve been playing with Monads and making an effort to
understand how they work and their usefulness in the greater context
of writing good software. A common example given is the Maybe monad in
Haskell. I believe the same concept is called Option in
Scala.
Commonly I would use Option as follows:
This works, but doesn’t leverage the fact that Option is a
Monad. Since it’s a Monad, we can easily extract the value and perform
simple compositions on the Option object.
According to the Scala documentation, this is the more idiomatic
approach when dealing with Option.