Created
June 17, 2014 05:00
-
-
Save dsugden/4ae2fbc2362337b95991 to your computer and use it in GitHub Desktop.
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 UseScalaz { | |
def main(args: Array[String]): Unit = { | |
import scalaz._ | |
case class Better(total: List[Int]) | |
def sum(i: Int): State[Better, Int] = State { b => | |
val result = if(b.total.length > 1) 99 else b.total.sum | |
val newBetter =b.copy(total = i :: b.total) | |
(newBetter, result) | |
} | |
val test = for { | |
r1 <- sum(1) // first result | |
state0 <- State.get[Better] // state after first result | |
r2 <- sum(2) // second result | |
state1 <- State.get[Better] // state after second result | |
r3 <- sum(3) // third result | |
state2 <- State.get[Better] // state after third result | |
} yield { | |
( | |
state0 :: state1 :: state2 :: Nil, // hey, I wanna see a list of the states as I did this, for debugging | |
r1 :: r2 :: r3 :: Nil // while I'm at it, lets look at a list of the results | |
) | |
} | |
val someBetter = Better(Nil) | |
// show the states | |
test.run(someBetter)._2._1.zipWithIndex.foreach { case (v, i) => println(s"state$i - $v")} | |
// show the results | |
test.run(someBetter)._2._2.zipWithIndex.foreach { case (v, i) => println(s"result$i - $v")} | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment