Read a text file and match lambdas

Report a typo

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.

Match the items from left and right columns
val operate: Iterator[String] => Unit = _.foreach(println)
val operate: Iterator[String] => List[String] = _.toList
val operate: Iterator[String] => Int = _.size
I like Scala!
I prefer functional programming style!
42!
3
List(I like Scala!, I prefer functional programming style!, 42!)
___

Create a free account to access the full topic