Skip to content

Instantly share code, notes, and snippets.

@missingfaktor
Created September 10, 2011 13:53
Show Gist options
  • Select an option

  • Save missingfaktor/1208334 to your computer and use it in GitHub Desktop.

Select an option

Save missingfaktor/1208334 to your computer and use it in GitHub Desktop.
Playing with Scalaz effects
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