Skip to content

Instantly share code, notes, and snippets.

@SteveBate
Last active August 29, 2015 14:24
Show Gist options
  • Save SteveBate/ff350dc240b637989d64 to your computer and use it in GitHub Desktop.
Save SteveBate/ff350dc240b637989d64 to your computer and use it in GitHub Desktop.
quick demo of reading csv data and mapping to a value object before printing out a property on each object
import scala.io.Source
// value object - automatically implements equals and gethashcode
case class Line(lineNo: Int, partNo: String, title: String, description: String)
object Test {
// converts string array into Line value object
def toLine(args: Array[String]): Line = {
Line(args(0).toInt, args(1), args(2), args(3))
}
def main(args: Array[String]): Unit = {
// read lines, convert each line to a Line value object, then print out part numbers
val file = Source.fromFile("data.csv")
file.getLines().map(str => toLine(str.split(", "))).foreach(l => println(l.partNo))
file.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment