Skip to content

Instantly share code, notes, and snippets.

@nisshiee
Created May 31, 2012 12:10
Show Gist options
  • Save nisshiee/2842998 to your computer and use it in GitHub Desktop.
Save nisshiee/2842998 to your computer and use it in GitHub Desktop.
BufferedReaderをIterateeで扱う
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