I am still fairly new to
Scala so take what I write in this article with a grain of salt.
The more I learn Scala
to more I like it and here is an example of why…
The following Scala Code
will read in a file (whos name is the first command line argument) and read in
every line as a String Array, convert it to a List[String] then convert that to
a List[Int] (this assumes the file has a
valid integer on each line. Then it
prints out the Int list adding +1 to each number as it prints it out.
import scala.io.Source
object ListStringToInt{
def main(args: Array[String]) {
val sList = Source.fromFile(args(0)).
getLines.toList.map((s: String) => s.toInt)
sList.foreach((x: Int) => println(x + 1))
}
}
|