Last active
December 30, 2015 10:09
-
-
Save pchlupacek/7813959 to your computer and use it in GitHub Desktop.
file parse example with scalaz-stream
This file contains hidden or 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 scalaz.stream.Process._ | |
import scalaz.stream.Process | |
import scalaz.stream.io | |
import scalaz.stream.process1 | |
import scala.util.matching.Regex | |
def matchRegex(pattern:Regex) : Process1[String,String] = | |
receive1[String,String]({ | |
case s@pattern(_) => emit(s) | |
case s => Halt(new Exception(s"Failed to parse $s, does not match regex: $pattern")) | |
}) | |
val trailer = "|TRAILER|(\\n+)".r | |
val lineOrTerm : Process1[String,String] = { | |
def go(lines:Int) = | |
matchRegex("(\\|[^\\|]*)+".r).flatMap { | |
case trailer(count) if count.toInt == lines => halt | |
case trailer(count) => Halt(new Exception(s"Expected $count lines, but got $lines")) | |
case s => emit(\/-(s)) fby go(lines+1) | |
} | |
go(0) | |
} | |
val source = | |
io.linesR("/some/file.txt" |> | |
buffer(buffersize) |> | |
(matchRegex("HEADER.*".r) fby matchRegex("(\\|[^\\|]*)+".r) fby lineOrTerm) |> | |
process1.utf8Encode | |
(source to io.fileChunkW("target/file.dat")).run.run | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment