Skip to content

Instantly share code, notes, and snippets.

@bmjames
Created March 9, 2012 11:51
Show Gist options
  • Save bmjames/2006228 to your computer and use it in GitHub Desktop.
Save bmjames/2006228 to your computer and use it in GitHub Desktop.
Scalaz Logger monad example
type StringLogger[A] = Logger[String, A]
def startWith(i: Int): StringLogger[Int] = i.logger :+-> "Started with " + i
def thenAdd(j: Int)(i: Int): StringLogger[Int] = (i + j).logger :+-> "Added " + j
def thenMultBy(j: Int)(i: Int): StringLogger[Int] = (i * j).logger :+-> "Multiplied by " + j
val loggedResult = startWith(1) >>= thenAdd(4) >>= thenMultBy(5)
loggedResult.over
// res0: Int = 25
loggedResult.log.toList
// res1: List[String] = List(Started with 1, Added 4, Multiplied by 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment