Created
May 31, 2012 12:10
-
-
Save nisshiee/2842998 to your computer and use it in GitHub Desktop.
BufferedReaderをIterateeで扱う
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 java.io.{ BufferedReader, FileReader } | |
import scalaz._, Scalaz._ | |
import scalaz.effects._ | |
import IterV._ | |
object App { | |
def main(args: Array[String]) { | |
val br = new BufferedReader(new FileReader("test2.txt")) | |
val io = byEnumerator(br) | |
io.unsafePerformIO | |
} | |
def byEnumerator(br: BufferedReader): IO[Unit] = { | |
val lineIter: IterV[String, Int] = head[String] map { | |
lineOpt => (lineOpt map (_.length)) | 0 | |
} | |
val iter: IterV[String, Stream[Int]] = repeat[String, Int, Stream](lineIter) | |
val enumerator: EnumeratorM[IO, String] = getReaderLines(br) | |
val read: IO[Int] = enumerator(iter) map { iter => | |
iter.run reduce (_ + _) | |
} | |
read map (_.toString) flatMap (putStrLn _) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment