Lightbend Activator

Play Framework with Scalaz monad transformers

Activator will be EOL-ed on May 24, 2017.

We’re making it easier and simpler for developers to get started with Lightbend technologies. This unfortunately means that future releases of Play, Akka and Scala will no longer include Activator support, and Lightbend’s Activator server will be decommissioned by the end of 2017. Instead of supporting Activator to create and set up development projects, we'll be supporting standard Giter8 templates for sbt users and Maven archetypes for Maven users. So going forward,

To create new Lightbend projects

Instead of using the Activator command, make sure you have sbt 0.13.13 (or higher), and use the “sbt new” command, providing the name of the template. For example, “$ sbt new akka/hello-akka.g8”. You can find a list of templates here.

Also, as a convenience, the Lightbend Project Starter allows you to quickly create a variety of example projects that you just unzip and run.

To create new templates

If you want to create new templates, you can now do that in Giter8.

To migrate templates from Activator to Giter8

If you created Activator templates in the past, please consider migrating them to Giter8 with this simple process.

Play Framework with Scalaz monad transformers

October 29, 2014
playframework scala scalaz

An example application that shows you how to use Scalaz monad transformers to untangle deeply nested code.

How to get "Play Framework with Scalaz monad transformers" on your computer

There are several ways to get this template.

Option 1: Choose play-monad-transformers in the Lightbend Activator UI.

Already have Lightbend Activator (get it here)? Launch the UI then search for play-monad-transformers in the list of templates.

Option 2: Download the play-monad-transformers project as a zip archive

If you haven't installed Activator, you can get the code by downloading the template bundle for play-monad-transformers.

  1. Download the Template Bundle for "Play Framework with Scalaz monad transformers"
  2. Extract the downloaded zip file to your system
  3. The bundle includes a small bootstrap script that can start Activator. To start Lightbend Activator's UI:

    In your File Explorer, navigate into the directory that the template was extracted to, right-click on the file named "activator.bat", then select "Open", and if prompted with a warning, click to continue:

    Or from a command line:

     C:\Users\typesafe\play-monad-transformers> activator ui 
    This will start Lightbend Activator and open this template in your browser.

Option 3: Create a play-monad-transformers project from the command line

If you have Lightbend Activator, use its command line mode to create a new project from this template. Type activator new PROJECTNAME play-monad-transformers on the command line.

Option 4: View the template source

The creator of this template maintains it at

Option 5: Preview the tutorial below

We've included the text of this template's tutorial below, but it may work better if you view it inside Activator on your computer. Activator tutorials are often designed to be interactive.

Preview the tutorial

Deeply nested code

For comprehensions are a great way to make code easier to read. However, they are not so useful when using nested containers. Suppose the we want to add two optional values in a Future, then we can not do the following:

val fx: Future[Option[Int]] = ???
val fy: Future[Option[Int]] = ???

for {
  x <- fx
  y <- fy
} yield x + y

This fails, because x and y are not Int but Option[Int], and these can't be added.

In this tutorial we'll work towards a solution. More background information can be found in the presentation 'Flatten your code' and the accompanying Github project.

Our sample application

The sample controller in Application1.scala. shows our hypothetical problem domain:

  • Get a username from the query string parameters
  • Lookup the user with that username
  • Validate that user's email address
  • Send the user an email

Each of these steps may fail, and the application must return a proper response code and description.

The code in Application1.scala is deeply nested. One of the biggest problems of this code is that the error handling for a particular method that may fail is often many lines away from that method. For example, when getUserName returns a None, we deal with that problem at the bottom of the action.

Monad transformers

As-is, we can't easily rewrite this to use a for-comprehension, because of the nested containers. But a monad transformer can wrap multiple container types and behave as a single container that exhibits the behaviours of all wrapped containers.

It's very instructive to write your own monad transformer to get a good feeling. For that, see the earlier linked presentation. Here, we will be using Scalaz monad transformers.

In the file Application2.scala we've rewritten our sample application to use the EitherT monad transformer.

The |> method is just function application, but with the function and its parameter reversed. So

foo |> bar |> quux

is equivalent to


We use this to neatly line up our code. In all lines in our for-comprehension, the most interesting stuff is happening at the left. These are the invocations to the UserService in our case. Slightly less interesting (and more to the right) are the errors that we generate if the previous method failed. Notice that these errors are much closer to the method that caused them than in the previous example.

Downright boring and completely to the right are conversions from various container types into the monad transformer that we use.

Easier lifting of the containers

In Application3.scala we've improved our code a bit more. We've created an HttpResult object that contain many functions to lift a value into our monad transformer. This makes it really easy to deal with many types of values.