I am writing a simple function called reduceByKey that takes a collection of (key, numeric) pairs and returns reduced collection by key.
def redu
If you are only interested in numeric values, you can use the standard Numeric type class and do this:
def reduceByKey[K,V](collection: Traversable[Tuple2[K, V]])(implicit num: Numeric[V]) = {
import num._
collection
.groupBy(_._1)
.map { case (group: K, traversable) => traversable.reduce{(a,b) => (a._1, a._2 + b._2)} }
}
The num implicit parameter serves as an evidence that V is a numeric type, and provides the + operation for this type.