Skip to content

Instantly share code, notes, and snippets.

@halcat0x15a
Created August 15, 2011 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halcat0x15a/1146060 to your computer and use it in GitHub Desktop.
Save halcat0x15a/1146060 to your computer and use it in GitHub Desktop.
scalaz.effectsを試したり
import scalaz._
import effects._
import Scalaz._
val i = for {
_ <- putStrLn("Greetings! What is your name?")
inpStr <- readLn
outStr = "Welcome to Scalaz, " |+| inpStr |+| "!"
_ <- putStrLn(outStr)
} yield ()
val i2 = putStrLn("Greetings! What is your name?") >|> readLn >>= { inpStr =>
val outStr = "Welcome to Scalaz, " |+| inpStr |+| "!"
putStrLn(outStr)
}
val i3 = for {
v <- newIORef("Scala")
_ <- v.write("Clojure")
_ <- v.mod(_ |+| "Tan")
s <- v.read
_ <- putStrLn(s)
} yield ()
import scalaz._
import effects._
import Scalaz._
import IterV._
val file = new java.io.File("test.txt")
val file2 = new java.io.File("test2.txt")
val i = for {
s <- for {
f <- getFileLines(file)(collect[String, List])
} yield f.run
outStr = s.mkString("\n")
_ <- putStrLn(outStr)
} yield ()
val i2 = for {
s <- for {
f <- getFileLines(file)(length)
f2 <- getFileLines(file2)(f)
} yield f2.run
_ <- putOut(s)
} yield ()
import scalaz._
import effects._
import Scalaz._
val f = new Forall[({type X[S] = ST[S, String]})#X] {
def apply[A] = for {
v <- newVar[A, String]("Scala")
_ <- v.mod(_ |+| "Chan")
s <- v.read
} yield s
}
val f2 = new Forall[({type X[S] = ST[S, ImmutableArray[String]]})#X] {
def apply[A] = for {
v <- newArr[A, String](5, "Scala")
_ <- v.write(1, "Clojure")
_ <- v.write(2, "Haskell")
_ <- v.write(3, "Python")
_ <- v.write(4, "JRuby")
a <- v.freeze
} yield a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment