title: Idiomatic way to reduce a list of pairs to a map of keys and their aggregated count?
body: <p>I'm trying to recreate Hadoop's <a href="http://hadoop.apache.org/docs/r1.1.1/mapred_tutorial.html">word count</a> map / reduce logic in a simple Scala program for learning</p>
  
  <p>This is what I have so far</p>
  
  <pre><code>val words1 = "Hello World Bye World"      
  val words2 = "Hello Hadoop Goodbye Hadoop"
  
  val input = List(words1,words2)           
  val mapped = input.flatMap(line=&gt;line.split(" ").map(word=&gt;word-&gt;1))
      //&gt; mapped  : List[(String, Int)] = List((Hello,1), (World,1), (Bye,1), 
      //                                       (World,1), (Hello,1), (Hadoop,1), 
      //                                       (Goodbye,1), (Hadoop,1))
  
  mapped.foldLeft(Map[String,Int]())((sofar,item)=&gt;{
      if(sofar.contains(item._1)){
          sofar.updated(item._1, item._2 + sofar(item._1))
      }else{
          sofar + item
      }
  })                              
      //&gt;Map(Goodbye -&gt; 1, Hello -&gt; 2, Bye -&gt; 1, Hadoop -&gt; 2, World -&gt; 2)
  </code></pre>
  
  <p>This seems to work, but I'm sure there is a more idiomatic way to handle the reduce part (foldLeft) </p>
  
  <p>I was thinking about perhaps a multimap,  but I have a feeling Scala has a way to do this easily </p>
  
  <p>Is there? e.g. <strong>a way to add to a map, and if the key exists, instead of replacing it, adding the value to the existing value</strong>. I'm sure I've seen this quesion somewhere, but couldn't find it and neither the answer.</p>
  
  <p>I know <code>groupBy</code> is the way to do it probably in the real world, but I'm trying to implement it as close as possible to the original map/reduce logic in the link above.</p>