I am learning Scala (coming from background mostly Java). I am trying to wrap my head around the following code:
object Main { def main(args : Array[String]) { for (file <- filesEnding(".txt")) println(file.getName) } private val filesHere = (new java.io.File(".")).listFiles def filesMatching(matcher: String => Boolean) = for (file <- filesHere; if matcher(file.getName)) yield file def filesEnding(query: String) = filesMatching(_.endsWith(query)) /* Other matcher functions */ }
In particular, I am confused when Scala gets the value for _ in each of the conjugate functions. I see that filesEnding is filesEnding called with the .txt argument. This argument is assigned to query . filesEnding then calls filesMatching with an argument consistent with String => Boolean . Finally, I see that file.getName is what ultimately replaces the _ placeholder.
I do not understand how Scala knows file.getName instead of _ . I'm having trouble tracking this code in my head, and the eclipse debugger doesn't help much in this situation. Can someone get me through what is going on in this code?
source share