I can write the following for Vectors:
def add[K,V](map: Map[K,Vector[V]], key: K, values: Vector[V]): Map[K,Vector[V]] = {
map + (key -> (map.getOrElse(key, Vector.empty) ++ values))
}
Usage:
scala> add(Map(1 -> Vector(1,2,3)), 1, Vector(4,5,6))
res1: Map[Int,Vector[Int]] = Map(1 -> Vector(1, 2, 3, 4, 5, 6))
The code for Sets is almost identical:
def add[K,V](map: Map[K,Set[V]], key: K, values: Set[V]): Map[K,Set[V]] = {
map + (key -> (map.getOrElse(key, Set.empty) ++ values))
}
How to make a single function that works for all Iterables? I tried to write it like this:
def add[K,V](map: Map[K,Iterable[V]], key: K, values: Iterable[V]): Map[K,Iterable[V]] = {
map + (key -> (map.getOrElse(key, Iterable.empty) ++ values))
}
But in this case I lost type info:
scala> add(Map(1 -> Set(1,2,3)), 1, Set(4,5,6))
res4: Map[Int,Iterable[Int]] = Map(1 -> Set(5, 1, 6, 2, 3, 4))
I tried the following:
def add[K,V,I[_] <: Iterable[_]](map: Map[K,I[V]], key: K, values: I[V]): Map[K,I[V]] = {
map + (key -> (map.get(key).map(_ ++ values).getOrElse(values)))
}
But it didn't compile:
Cannot construct a collection of type That with elements of type Any based on a collection of type Repr. [error] map + (key -> (map.get(key).map(_ ++ values).getOrElse(values)))