Created
August 14, 2012 01:57
-
-
Save puffnfresh/3345722 to your computer and use it in GitHub Desktop.
Example of scalaz' WriterT for Atlassian (pure functional logging)
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
import scalaz.WriterT | |
import scalaz.NonEmptyList | |
import scalaz.syntax.id._ | |
import scalaz.std.option._ | |
import scalaz.syntax.std.option._ | |
type OptionLogger[A] = WriterT[Option, NonEmptyList[String], A] | |
val two: OptionLogger[Int] = WriterT.put(2.some)("The number two".wrapNel) | |
val hundred: OptionLogger[Int] = WriterT.put(100.some)("One hundred".wrapNel) | |
val twoHundred = for { | |
a <- two | |
b <- hundred | |
} yield a * b | |
println(twoHundred.value) | |
// Some(200) | |
twoHundred.written map { _.list } getOrElse List() foreach { println _ } | |
// The number two | |
// One hundred | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment