A pattern that appears sometimes in functional programming is that of a function first computing some kind of intermediate result and then passing that result to another function which was passed in as an argument, in order to delegate the computation of the final result.

For example:

case class User(id: Int, name: String, age: Int)
sealed abstract class UserUpdateResult
case class Succeeded(updatedUserId: Int) extends UserUpdateResult
case object Failed extends UserUpdateResult
import cats.Eval
// import cats.Eval

def updateUser(persistToDatabase: User => Eval[UserUpdateResult])
              (existingUser: User, newName: String, newAge: Int): Eval[UserUpdateResult] = {
  val trimmedName = newName.trim
  val cappedAge = newAge max 150
  val updatedUser = existingUser.copy(name = trimmedName, age = cappedAge)

// updateUser: (persistToDatabase: User => cats.Eval[UserUpdateResult])(existingUser: User, newName: String, newAge: Int)cats.Eval[UserUpdateResult]

(Note: We will be using Eval throughout the examples on this page. If you are not familiar with Eval, it’s worth reading the Eval documentation first.)

Our updateUser function takes in an existing user and some updates to perform. It sanitises the inputs and updates the user model, but it delegates the database update to another function which is passed in as an argument.

This pattern is known as “continuation passing style” or CPS, and the function passed in (persistToDatabase) is known as a “continuation”.

Note the following characteristics:

  • The return type of our updateUser function (Eval[UserUpdateResult]) is the same as the return type of the continuation function that was passed in.
  • Our function does a bit of work to build an intermediate value, then passes that value to the continuation, which takes care of the remainder of the work.

In Cats we can encode this pattern using the ContT data type:

// import

def updateUserCont(existingUser: User,
                   newName: String,
                   newAge: Int): ContT[Eval, UserUpdateResult, User] =
  ContT.apply[Eval, UserUpdateResult, User] { next =>
    val trimmedName = newName.trim
    val cappedAge = newAge min 150
    val updatedUser = existingUser.copy(name = trimmedName, age = cappedAge)

// updateUserCont: (existingUser: User, newName: String, newAge: Int)[cats.Eval,UserUpdateResult,User]

We can construct a computation as follows:

val existingUser = User(100, "Alice", 42)
// existingUser: User = User(100,Alice,42)

val computation = updateUserCont(existingUser, "Bob", 200)
// computation:[cats.Eval,UserUpdateResult,User] = FromFn(AndThen$770319361)

And then call run on it, passing in a function of type User => Eval[UserUpdateResult] as the continuation:

val eval = { user =>
  Eval.later {
    println(s"Persisting updated user to the DB: $user")
// eval: cats.Eval[UserUpdateResult] = cats.Later@7730ee40

Finally we can run the resulting Eval to actually execute the computation:

// Persisting updated user to the DB: User(100,Bob,150)
// res0: UserUpdateResult = Succeeded(100)


You might be wondering what the point of all this was, as the function that uses ContT seems to achieve the same thing as the original function, just encoded in a slightly different way.

The point is that ContT is a monad, so by rewriting our function into a ContT we gain composibility for free.

For example we can map over a ContT:

val anotherComputation = { user =>
    "id" ->,
    "name" ->,
    "age" -> user.age.toString
// anotherComputation:[cats.Eval,UserUpdateResult,scala.collection.immutable.Map[String,String]] = FromFn(AndThen$1953253416)

val anotherEval = { userFields =>
  Eval.later {
    println(s"Persisting these fields to the DB: $userFields")
// anotherEval: cats.Eval[UserUpdateResult] = cats.Eval$$anon$5@2a2a8ea9

// Persisting these fields to the DB: Map(id -> 100, name -> Bob, age -> 150)
// res1: UserUpdateResult = Succeeded(100)

And we can use flatMap to chain multiple ContTs together.

The following example builds 3 computations: one to sanitise the inputs and update the user model, one to persist the updated user to the database, and one to publish a message saying the user was updated. It then chains them together in continuation-passing style using flatMap and runs the whole computation.

val updateUserModel: ContT[Eval, UserUpdateResult, User] =
  updateUserCont(existingUser, "Bob", 200).map { updatedUser =>
    println("Updated user model")
// updateUserModel:[cats.Eval,UserUpdateResult,User] = FromFn(AndThen$496617510)

val persistToDb: User => ContT[Eval, UserUpdateResult, UserUpdateResult] = {
  user =>
    ContT.apply[Eval, UserUpdateResult, UserUpdateResult] { next =>
      println(s"Persisting updated user to the DB: $user")

// persistToDb: User =>[cats.Eval,UserUpdateResult,UserUpdateResult] = $$Lambda$11067/439862813@33c146da

val publishEvent: UserUpdateResult => ContT[Eval, UserUpdateResult, UserUpdateResult] = {
  userUpdateResult =>
    ContT.apply[Eval, UserUpdateResult, UserUpdateResult] { next =>
      userUpdateResult match {
        case Succeeded(userId) =>
          println(s"Publishing 'user updated' event for user ID $userId")
        case Failed =>
          println("Not publishing 'user updated' event because update failed")

// publishEvent: UserUpdateResult =>[cats.Eval,UserUpdateResult,UserUpdateResult] = $$Lambda$11068/97815827@4fd6ae2d

val chainOfContinuations =
  updateUserModel flatMap persistToDb flatMap publishEvent
// chainOfContinuations:[cats.Eval,UserUpdateResult,UserUpdateResult] = FromFn(AndThen$946122708)

val eval = { finalResult =>
  Eval.later {
// eval: cats.Eval[UserUpdateResult] = cats.Eval$$anon$5@737c497e

// Updated user model
// Persisting updated user to the DB: User(100,Bob,150)
// Publishing 'user updated' event for user ID 100
// Finished!
// res2: UserUpdateResult = Succeeded(100)

Why Eval?

If you’re wondering why we used Eval in our examples above, it’s because the Monad instance for ContT[M[_], A, B] requires an instance of cats.Defer for M[_]. This is an implementation detail - it’s needed in order to preserve stack safety.

In a real-world application, you’re more likely to be using something like cats-effect IO, which has a Defer instance.