题目为:用纯scala 统计一个文件夹下面所有的文件或子目录下的文件中每个单词出现的总次数
/**
* Created by corleone on 2016/1/3.
*/
object functional_3 extends App {
val path = "d:\\functional"
def getFiles(fileName: String): List[String] = {
val file = new java.io.File(fileName)
if (!file.exists) {
List()
} else if (file.isDirectory) {
file.listFiles.filter(_.canRead).map(_.getAbsolutePath).map(getFiles).flatten.toList
} else {
List(fileName)
}
}
val new_fun = getFiles _ // 无意义,纯粹 体验有参函数赋给另一个变量的用法
val words = getFiles(path).flatMap(scala.io.Source.fromFile(_).getLines().mkString(" ").split("\\s"))
words.groupBy(t => t).foreach(listGroupByWord => {
println(listGroupByWord._1 + " size:" + listGroupByWord._2.size)
})
// 高阶函数
// 函数式编程
// currying
// 闭包
}