Created
August 31, 2012 16:25
-
-
Save debasishg/3555396 to your computer and use it in GitHub Desktop.
State monad in scalaz
This file contains hidden or 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
scala> state[Int, Int](10) | |
res9: scalaz.State[Int,Int] = scalaz.package$State$$anon$1@3bbe242c | |
scala> res9.run(0) | |
res10: (Int, Int) = (0,10) | |
scala> res9 map (_ * 2) | |
res11: scalaz.StateT[scalaz.Id.Id,Int,Int] = scalaz.StateT$$anon$7@31a9253 | |
scala> res11.run(0) | |
res12: (Int, Int) = (0,20) | |
scala> res9 flatMap {a => state(a * 3)} | |
res14: scalaz.StateT[scalaz.Id.Id,Int,Int] = scalaz.StateT$$anon$7@18ee131f | |
scala> res14.run(12) | |
res15: (Int, Int) = (12,30) | |
scala> for { | |
| r <- res14 | |
| p <- state(r * 8) | |
| } yield p | |
res19: scalaz.StateT[scalaz.Id.Id,Int,Int] = scalaz.StateT$$anon$7@5ef21bc4 | |
scala> res19.run(0) | |
res20: (Int, Int) = (0,240) | |
scala> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment