object Futures4 extends App {
// get an array of all files in a directory tree
def recursiveListFiles(f: File): Array[File] = {
val these = f.listFiles
these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles)
}
val files = recursiveListFiles(new java.io.File("path"))
// convert to a list of full file paths
val fileNames = files.toList map (_.getAbsolutePath)
println(fileNames)
val n: Int = fileNames.length
val futures = for {
i <- 0 to n-1
} yield future { line_num(i) }
// read a file by line
def line_num(i: Int) : Int =
{
for
{
line <- Source.fromFile(fileNames(i)).getLines
//here i am getting multiple errors saying type mismatch
}yield line.length
}
val sumF = Future.fold(futures)(0)(_ + _)
val sum = Await.result(sumF, 2 seconds)
println(sum)
}
我想计算所有文件中的行数。我是Scala编程的新手,所以任何帮助都将不胜感激!我在一步提到多个错误,我已经提到过评论。感谢
答案 0 :(得分:0)
line_num(i)
返回文件i
中的行数。所以你可以写下来:
def line_num(i: Int) : Int = {
Source.fromFile(fileNames(i)).getLines.size
}
在原始函数中,返回Seq(Int)
,它是文件中每行长度的序列,与预期的返回类型不对应。