Search code examples
scalafunctional-programmingmapsscalaz

Merge maps by key


Say I have two maps:

val a = Map(1 -> "one", 2 -> "two", 3 -> "three")
val b = Map(1 -> "un", 2 -> "deux", 3 -> "trois")

I want to merge these maps by key, applying some function to collect the values (in this particular case I want to collect them into a seq, giving:

val c = Map(1 -> Seq("one", "un"), 2 -> Seq("two", "deux"), 3 -> Seq("three", "trois"))

It feels like there should be a nice, idiomatic way of doing this.


Solution

  • scala.collection.immutable.IntMap has an intersectionWith method that does precisely what you want (I believe):

    import scala.collection.immutable.IntMap
    
    val a = IntMap(1 -> "one", 2 -> "two", 3 -> "three", 4 -> "four")
    val b = IntMap(1 -> "un", 2 -> "deux", 3 -> "trois")
    
    val merged = a.intersectionWith(b, (_, av, bv: String) => Seq(av, bv))
    

    This gives you IntMap(1 -> List(one, un), 2 -> List(two, deux), 3 -> List(three, trois)). Note that it correctly ignores the key that only occurs in a.

    As a side note: I've often found myself wanting the unionWith, intersectionWith, etc. functions from Haskell's Data.Map in Scala. I don't think there's any principled reason that they should only be available on IntMap, instead of in the base collection.Map trait.