Skip to content

Instantly share code, notes, and snippets.

@lehrblogger
Created June 11, 2013 20:01
Show Gist options
  • Save lehrblogger/5760112 to your computer and use it in GitHub Desktop.
Save lehrblogger/5760112 to your computer and use it in GitHub Desktop.
def readAllFiles(fileNames: List[String]): Map[String, Map[String, Int]] = {
val wordsInFiles: List[(String, String)] = for {
filename <- fileNames
line <- filename.toFile.readLines()
word <- line.split("\\W+|\\d+")
} yield (word.toLowerCase.trim, filename)
val emptyMap: Map[String, Map[String, Int]] = Map.empty.withDefaultValue(Map.empty.withDefaultValue())
wordsInFiles.foldLeft(emptyMap) { case (map, (word, filename)) =>
map.update(word, map.apply(word).update(filename, map.apply(word).apply(filename) + 1))
//map.update(word, map.apply(word).update(filename, map(word)(filename) + 1))
//map.update(word, map.apply(word)(filename) = map(word)(filename) + 1)
//map.update(word, map(word)(filename) = map(word)(filename) + 1)
//map.update(word, map(word)(filename) += 1)
//map(word) = (map(word)(filename) += 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment