Skip to content

Instantly share code, notes, and snippets.

@nisshiee
Created February 19, 2012 00:29
Show Gist options
  • Save nisshiee/1861448 to your computer and use it in GitHub Desktop.
Save nisshiee/1861448 to your computer and use it in GitHub Desktop.
scalaz Iterateeでファイルを読み込む練習
package iteratee.practice
import scalaz._
import Scalaz._
import effects._
import java.io.File
/**
* http://code.google.com/codejam/contest/1343486/dashboard
* の問題ファイルを読み込んでみる練習
*/
object Practice1 extends App {
case class Problem(n: Int, k: Int)
lazy val probRegex = """^(\d+) +(\d+)$""".r
def line2Prob: String => Option[Problem] = {
case probRegex(n, k) => (for {
ni <- n.parseInt
ki <- k.parseInt
} yield Problem(ni, ki)).toOption
case _ => None
}
def headProb = IterV.head[String] map (_ >>= line2Prob)
def repeatProb = IterV.repeat[String, Option[Problem], List](headProb) map (_.flatten)
def readProb = IterV.drop(1) >>=| repeatProb
val f = new File("practice/1-2.in");
val lines = getFileLines(f)
val io = lines(readProb) >>= { iterv =>
val itervio = iterv.run map (p => putStrLn(p.toString))
itervio.sequence[IO, Unit] map (_ => ())
}
io.unsafePerformIO
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment