高阶函数可以把其它函数当作函数参数,帮助我们减少代码重复,例如:
object FileMatcher { private def fileHere = (new File(".\file").listFiles()) def fileEnding(query : String) = { for(file <- fileHere if file.getName.endsWith(query)) yield file } def fileMathching(query: String, matcher:(String, String) => Boolean) = { for(file <- fileHere if matcher(file.getName, query)) yield file } def fileEnding2(query : String) = fileMathching(query, _.endsWith(_)) def fileContains(query : String) = fileMathching(query, _.contains(_)) def main(args: Array[String]): Unit = { fileEnding2(".scala").foreach { println } } }