Skip to content

Instantly share code, notes, and snippets.

@talonx
Last active April 27, 2017 15:29
Show Gist options
  • Save talonx/a36a1d34de77a46230f413b7337b5a3d to your computer and use it in GitHub Desktop.
Save talonx/a36a1d34de77a46230f413b7337b5a3d to your computer and use it in GitHub Desktop.
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment