Suppose you have
val docs = List(List(\"one\", \"two\"), List(\"two\", \"three\"))
where e.g. List(\"one\", \"two\") represents a document
docs.flatten.foldLeft(new Map.WithDefault(Map[String,Int](),Function.const(0))){ (m,x) => m + (x -> (1 + m(x)))}
What a train wreck!
[Edit]
Ah, that's better!
docs.flatten.foldLeft(Map[String,Int]() withDefaultValue 0){ (m,x) => m + (x -> (1 + m(x)))}