Question

def recursiveTotalDirectorySize(dir: File): Long = {
    var totalFileSize = 0L;
    val (dirs, files) = dir.listFiles.partition(_.isDirectory)

    if (dirs.length > 0) dirs.foreach((f: File) => totalFileSize += recursiveTotalDirectorySize(f))

    if (files.length > 0) totalFileSize += files.map((f: File) => f.length()).reduceLeft(_ + _)

    totalFileSize
}

Do you have a better solution for total file size recursion using Scala. What are your comments for this. Thanks!

Was it helpful?

Solution

One liner, no mutable vars, parallelizes the processing of each entry in a directory

  def recursiveTotalDirectorySize(dir: File): Long =
  dir.listFiles.par.map{
     f => if (f.isDirectory) recursiveTotalDirectorySize(f) else f.length()
  }.sum

OTHER TIPS

A similar approach, using foldLeft,

def dirSize(dir: File): Long = 
  ( 0L /: dir.listFiles) { (sum,f) => sum + 
    (if (f.isDirectory) dirSize(f) else f.length) }

Accepted answer has the potential quality of using par.map; here par.aggregate may prove equivalent,

def dirSize(dir: File): Long = 
  dir.listFiles.par.aggregate(0L)((sum,f) => sum + 
    (if (f.isDirectory) dirSize(f) else f.length), _+_)

In fact par.aggregate may be seen as a form of MapReduce.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top