Let's assume that data.txt file has the following content:
I like Scala!
I prefer functional programming style!
42!
With the withSourceReader method:
val filePath = "data.txt"
def withSourceReader[A](filePath: String)(operate: Iterator[String] => A): A =
val source = Source.fromFile(filePath)(Codec.UTF8)
val lines = source.getLines()
try operate(lines)
finally source.close()
Use lambda operate from our withSourceReader method for extracting information as requested in the following tests below.