Skip to content

Instantly share code, notes, and snippets.

@puffnfresh
Created August 14, 2012 01:57
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save puffnfresh/3345722 to your computer and use it in GitHub Desktop.
Save puffnfresh/3345722 to your computer and use it in GitHub Desktop.
Example of scalaz' WriterT for Atlassian (pure functional logging)
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