Created
August 15, 2011 11:26
-
-
Save halcat0x15a/1146060 to your computer and use it in GitHub Desktop.
scalaz.effectsを試したり
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
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 () |
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
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 () |
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
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