简体   繁体   中英

Closures in Scala

I'm trying to learn Scala and I can't understand this example. In listing 9.1 of Programming in Scala, by Odersky et. al., the authors produce this code

object FileMatcher {
  private def filesHere = (new java.io.File(".")).listFiles
  private def filesMatching(matcher: String => Boolean) =
    for (file <- filesHere; if matcher(file.getName))
      yield file
  def filesEnding(query: String) =
    filesMatching(_.endsWith(query))
  def filesContaining(query: String) =
    filesMatching(_.contains(query))
  def filesRegex(query: String) =
    filesMatching(_.matches(query))
}

They give a scenario where you are writing a FileMatcher object to be used by client code written by others, and this code is the result of a couple of refactorings.

I understand that query is a free variable, but I don't understand how the caller is supposed to make use of it. Since Scala is, if I understand correctly, lexically scoped, and this is an object definition, the client can't define query in a lexically enclosing scope, so where is query to come from?

Can you give me an example of how a client is supposed to call filesEnding to find all files ending in ".txt" for example?

Try it.

scala> object FileMatcher {
     |   private def filesHere = (new java.io.File(".")).listFiles
     |   private def filesMatching(matcher: String => Boolean) =
     |     for (file <- filesHere; if matcher(file.getName))
     |       yield file
     |   def filesEnding(query: String) =
     |     filesMatching(_.endsWith(query))
     |   def filesContaining(query: String) =
     |     filesMatching(_.contains(query))
     |   def filesRegex(query: String) =
     |     filesMatching(_.matches(query))
     | }
defined module FileMatcher

scala> FileMatcher filesEnding "xml"
res7: Array[java.io.File] = Array(./build.examples.xml, ./build.xml, ./build.detach.xml)

scala> FileMatcher filesContaining "example"
res8: Array[java.io.File] = Array(./build.examples.xml)

If you have further questions, please add them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM