First of all, you can simplify the map to return 0
when no key is present:
val map0 = Map.empty[Int, Int] withDefaultValue 0
Then you can safely call map(key)
instead of map.getOrElse(key, 0)
.
Second, you can use the arrow-association syntax to create Tuple2
instances. I.e. key -> value
instead of Tuple2(key, value)
.
Third, I would not introduce the if ... then ... else
at all. Just update the map for each key successively:
def addKey(map: Map[Int, Int], key: Int) = map + (key -> (map(key) + 1))
val map1 = addKey(map0, key1)
val map2 = addKey(map1, key2)
tailRecFunction(someArg, map2)