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.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.