An example Scala ‘App’ for the Cats “IO Monad” article

As a quick note, if you’re interested in using the IO monad described in this IO Monad for Cats article, here’s the source code for a complete Scala App based on that article:

import cats.effect.IO

object Program extends App {

    val program = for {
        _    <- IO { println("Welcome to Scala!  What's your name?") }
        name <- IO { scala.io.StdIn.readLine }
        _    <- IO { println(s"Well hello, $name!") }
    } yield ()

    program.unsafeRunSync() 
}

And here’s a build.sbt file you can use to run that App with SBT:

name := "CatsIOMonadExample"

version := "0.1"

scalaVersion := "2.12.2"

libraryDependencies ++= Seq(
    "org.typelevel" %% "cats" % "0.9.0",
    "org.typelevel" %% "cats-effect" % "0.3"
)

Running the App looks like this:

> run
[info] Compiling 1 Scala source ...
[info] Running Program 
Welcome to Scala!  What's your name?
Fred
Well hello, Fred!

See that article for more information. I just wanted to show a complete example, including the necessary SBT dependencies.

August, 2019 update: The Cats dependencies are now:

libraryDependencies ++= Seq(
   "org.typelevel" %% "cats-core" % "2.0.0-RC1",
   "org.typelevel" %% "cats-effect" % "1.3.1"
)