Created
March 9, 2012 11:51
-
-
Save bmjames/2006228 to your computer and use it in GitHub Desktop.
Scalaz Logger monad example
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
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