2

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?

4
  • 4
    FileMatcher.filesEnding("txt")
    – Apocalisp
    Jan 18, 2012 at 16:22
  • this way you don't call directly using a closure; however the method you call use a closure: _.endsWith(query)
    – thoredge
    Jan 18, 2012 at 17:15
  • 1
    query is just a completely standard method parameter.
    – soc
    Jan 18, 2012 at 23:30
  • As this is a possible duplicate of at least two questions, you might want to look at this explanation. HTH!
    – fotNelton
    Jan 19, 2012 at 7:20

1 Answer 1

6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.