Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.collection.mutable.Map
- object reduce {
- def reduceByKey(func: (Int, Int) => Int, pairs: List[(String, Int)]): Map[String, Int] = {
- val ret = Map[String, Int]()
- for (tup <- pairs) {
- val k = tup._1
- val v = tup._2
- val nv = ret.getOrElse(k, 0)
- ret(k) = func(nv, v)
- }
- ret
- }
- def main(args :Array[String]) {
- val l = List("the quick brown fox", "jumps over the lazy dog", "how mumbo jumbo", "took over the world", "the world is flat")
- val fm = l.flatMap(line => line.split(" ")).map(w => (w, 1))
- val fin = reduceByKey((a, b) => (a + b), fm)
- println(fin)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement