The following code sums the values of the two maps into a single map.
val merged = (map1 /: map2) { case (map, (k,v)) =>
map + ( k -> (v + map.getOrElse(k, 0)) )
}
However I am unsuccessful in converting it using the foldLeft()()
function.
Here's what I tried , but not able to meaningfully progress.
val merged2 = map2 foldLeft (map1) ((acc:Map[Int,Int], (k:Int,v:Int)) => acc + ( k -> (v + acc.getOrElse(k, 0)) ))
Whats the correct way to rewrite using the foldLeft function?