Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. import scala.collection.mutable.Map
  2.  
  3. object reduce {
  4.  
  5. def reduceByKey(func: (Int, Int) => Int, pairs: List[(String, Int)]): Map[String, Int] = {
  6. val ret = Map[String, Int]()
  7.  
  8. for (tup <- pairs) {
  9. val k = tup._1
  10. val v = tup._2
  11. val nv = ret.getOrElse(k, 0)
  12. ret(k) = func(nv, v)
  13. }
  14. ret
  15. }
  16.  
  17.  
  18. def main(args :Array[String]) {
  19. val l = List("the quick brown fox", "jumps over the lazy dog", "how mumbo jumbo", "took over the world", "the world is flat")
  20. val fm = l.flatMap(line => line.split(" ")).map(w => (w, 1))
  21. val fin = reduceByKey((a, b) => (a + b), fm)
  22. println(fin)
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement