Skip to content

Instantly share code, notes, and snippets.

@virasak
Created December 30, 2009 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virasak/265899 to your computer and use it in GitHub Desktop.
Save virasak/265899 to your computer and use it in GitHub Desktop.
Remove projection if you don't want to println(dir)
import java.io.{Closeable, File, FileWriter, PrintWriter}
val rootDir = new File("/home/virasak/.vim")
if (!rootDir.exists) throw new IllegalArgumentException(rootDir + " does not exist")
val counts = scala.collection.mutable.Map.empty[String, Int]
for {
dir <- rootDir.listFiles.projection if dir.isDirectory && println("Processing " + dir) == ()
file <- dir.listFiles if file.isFile
line <- scala.io.Source.fromFile(file, "ISO-8859-1").getLines
word <- line.split("""\W+""").map(_.toLowerCase)
} counts(word) = counts.getOrElse(word, 0) + 1
val countList = counts.toList
println("Writing counts in decreasing order")
write(countList.sort(_._2 > _._2), "counts-descreasing-scala")
println("Writing counts in alphabetical order")
write(countList.sort(_._1 < _._1), "counts-alphabetical-scala")
def write(counts: List[(String,Int)], fileName: String) {
using(new PrintWriter(new FileWriter(fileName))) { out =>
for ((word, count) <- counts) out.println(word + "\t" + count)
}
}
def using[A <: Closeable, B](a: A)(f: A => B) = try { f(a) } finally { a.close }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment