Last active
August 27, 2021 05:27
-
-
Save ceedubs/8faea28f278dc1db07d7 to your computer and use it in GitHub Desktop.
Using the Writer monad for log messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object WriterExample extends App { | |
import scalaz._ | |
// DList has constant time append, which is nice for logging | |
// for simplicity we will just use String for logs, but you could | |
// get fancier and have a model with Error, Warn, Info, Debug, etc. | |
type Logged[A] = Writer[DList[String], A] | |
def log(msg: String): Logged[Unit] = WriterT.tell(DList(msg)) | |
val r = for { | |
_ <- log("start") | |
first = 1 | |
_ <- log(s"initialized first: $first") | |
second = 2 | |
_ <- log(s"initialized second: $second") | |
_ <- log("finish") | |
} yield first + second | |
val (logs, sum) = r.run | |
logs.toList.foreach(println) // print the logs | |
println(s"sum: $sum") // print the result | |
// this app prints the following output: | |
// start | |
// initialized first: 1 | |
// initialized second: 2 | |
// finish | |
// sum: 3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment