O'Reilly logo

Scala Design Patterns - Second Edition by Ivan Nikolov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using monads

Scalaz defines quite a lot of different methods that can be applied directly to any monad we have. There are multiple examples that show lists and options. The library also has a Monad trait that can be extended. It is similar to the Monoid trait.

We don't want to dig into list examples that show how to use monads, though. To make things interesting, let's look at the IO monad in Scalaz that can be used to perform I/O in a monadic way. What this basically means is that we can describe and compose those actions without actually performing them. This will lead to better code reuse as well. Let's see an example:

import java.io.{PrintWriter, File}import scala.io.Sourcepackage object monads {  def readFile(path: String) = { System. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required