Created
April 4, 2014 01:07
-
-
Save ezhulenev/9966059 to your computer and use it in GitHub Desktop.
'Functional' CSV parsing: http://stackoverflow.com/questions/22846375/a-more-functional-implementation
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.ByteArrayInputStream | |
import scalaz.stream.io | |
import scalaz.std.map._ | |
import scalaz.std.vector._ | |
object CsvParsing extends App { | |
val csv = | |
"""|0,2014-01-01,1 | |
|0,2014-01-02,2 | |
|1,2014-01-01,3 | |
|1,2014-01-01,4""".stripMargin | |
val is = new ByteArrayInputStream(csv.getBytes) | |
val process = io.linesR(is). | |
map(_.split(",")). // split to columns | |
map(arr => (arr(0), arr(2))). // pick id & number columns | |
runFoldMap { case (id, number) => Map(id -> Vector(number)) } | |
val output = process.run | |
println(s"Output: ") | |
output.foreach(println) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment