Created
September 10, 2011 13:53
-
-
Save missingfaktor/1208334 to your computer and use it in GitHub Desktop.
Playing with Scalaz effects
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
| import scalaz._ | |
| import Scalaz._ | |
| import effects._ | |
| object ExperimentingWithIo { | |
| def main(args: Array[String]): Unit = { | |
| val i1: IO[Unit] = for { | |
| _ <- putStrLn("What's your name?") | |
| inputStr <- readLn | |
| outputStr = "Hello " |+| inputStr |+| "!" | |
| _ <- putStrLn(outputStr) | |
| } yield () | |
| i1.unsafePerformIO | |
| val i2: IO[Unit] = putStrLn("What's your name?") >|> readLn >>= { inputStr => | |
| val outputStr = "Hello " |+| inputStr |+| "!" | |
| putStrLn(outputStr) | |
| } | |
| i2.unsafePerformIO | |
| val i3: IO[Unit] = for { | |
| v <- newIORef(5) | |
| _ <- v.write(9) | |
| _ <- v.mod(_ + 3) | |
| s <- v.read | |
| _ <- putStrLn(s.shows) | |
| } yield () | |
| i3.unsafePerformIO | |
| val f = new Forall[({type l[X] = ST[X, String]})#l] { | |
| def apply[A] = for { | |
| v <- newVar[A, String]("Scala") | |
| _ <- v.mod(_ |+| "ble") | |
| s <- v.read | |
| } yield s | |
| } | |
| runST(f) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment