Count lines of scala code (omitting empty lines) in a directory, traversing any subdirectories I need to simply count lines of Scala code in a project, which includes some package(i.e. directory) hierarchy.

I'm not sure about the performance of the code below. Can you help improve it, if possible?

Also, in Java, we have reduce() in the Stream API, and I didn't find its counterpart in the Scala library, so ended with foldLeft().

import java.io.File
import scala.io.Source

object CountLoc {
  def main(args: Array[String]): Unit = {
    val parentDir: File = new File(System.getProperty("user.dir"))

    def traverse(dir: File): Int = {
      val all = dir.list().map(f =&gt; new File(dir.getAbsolutePath + "/" + f))
      val srcs = all.filter(!_.isDirectory).filter(_.getName.endsWith(".scala"))
      val dirs = all.filter(_.isDirectory)
      srcs.foldLeft(0)((i, f) =&gt; i + Source.fromFile(f).getLines().filterNot(_.isEmpty).toList.size) +
        dirs.toList.foldLeft(0)((i,d) =&gt; i + traverse(d))
    }

    println("LOC: " + traverse(parentDir))
  }
}
Instead of collecting all the source files in one sweep, and all the sub-directories in another sweep, you could move the foldLeft() to the top level and deal with each element as it is encountered.

def traverse(dir :File) :Int =
  dir.listFiles().foldLeft(0){ case (sum,file) =&gt;
    if (file.isDirectory)
      sum + traverse(file)
    else if (file.getName.endsWith(".scala"))
      sum + io.Source.fromFile(file).getLines().count(_.nonEmpty)
    else
      sum
  }