mutable.Map deep merge

Dec 01 2020

C'è un modo conciso per unire profondamente due mappe mutabili in Scala?

case class K1(i: Int)
case class K2(i: Int)

def deepMerge(map: mutable.Map[K1, Map[K2, List[Int]]],
              mergee: mutable.Map[K1, Map[K2, List[Int]]]
): Unit = ???

Esempi:

IO.

val map = mutable.Map(K1(1) -> Map(K2(1) -> List(1)))
val mergee = mutable.Map(K1(1) -> Map(K2(1) -> List(2)))

deepMerge(map, mergee)
map = mutable.Map(K1(1) -> Map(K2(1) -> List(1, 2)))

II.

val map = mutable.Map(K1(1) -> Map(K2(1) -> List(1)))
val mergee = mutable.Map(K1(1) -> Map(K2(2) -> List(1)))

deepMerge(map, mergee)
map = mutable.Map(K1(1) -> Map(K2(1) -> List(1), K2(2) -> List(1)))

III.

val map = mutable.Map(K1(1) -> Map(K2(1) -> List(1)))
val mergee = mutable.Map(K1(2) -> Map(K2(2) -> List(1)))

deepMerge(map, mergee)
map = mutable.Map(K1(1) -> Map(K2(1) -> List(1)), K1(2) -> Map(K2(2) -> List(1)))

Ad esempio, se è presente la stessa chiave in entrambe le mappe, i valori a cui corrispondono le chiavi ( List[Int]) vengono uniti.

C'è un modo per implementarlo in modo conciso evitando di controllare se la particolare chiave è presentata o meno in un'altra mappa? Anche l'uso di librerie FP come scalaz o cats va bene.

Risposte

3 KrzysztofAtłasik Dec 01 2020 at 15:40

Aggiungo un'altra risposta usando i gatti.

Quello che stai descrivendo è in realtà il comportamento dei gatti . Quindi potresti semplicemente usare l' |+|operatore combinare ( ) per unire in profondità le mappe:

import cats.implicits._
import cats._

case class K1(i: Int)
case class K2(i: Int)

val map = Map(K1(1) -> Map(K2(1) -> List(1)))
val mergee = Map(K1(1) -> Map(K2(1) -> List(2)))

val deepMerged = map |+| mergee

println(deepMerged) // HashMap(K1(1) -> HashMap(K2(1) -> List(1, 2)))

Il problema è che cats lib non fornisce un'istanza di Semigroup per mutable.Map, ma potresti derivarla da uno per immutabile:

import cats.implicits._
import scala.collection.immutable
import scala.collection.mutable
import cats._

//here I derivive Semigroup instance for mutable.Map from instance for immutable.Map
implicit def mutableMapSemigroup[K, V: Semigroup]: Semigroup[mutable.Map[K, V]] = Semigroup[immutable.Map[K, V]].imap(c => mutable.Map.from(c))(c => immutable.Map.from(c))

case class K1(i: Int)
case class K2(i: Int)

val map = mutable.Map(K1(1) -> mutable.Map(K2(1) -> List(1)))
val mergee = mutable.Map(K1(1) -> mutable.Map(K2(1) -> List(2)))

println(map |+| mergee)

Ma tieni presente che questo converte effettivamente la mappa mutabile in immutabile, quindi si fonde e poi converte di nuovo nella mappa mutabile, quindi probabilmente non è molto efficiente.

1 jwvh Dec 01 2020 at 14:29

Questo potrebbe farlo.

def deepMerge(mergeA: Map[K1, Map[K2, List[Int]]],
              mergeB: Map[K1, Map[K2, List[Int]]]
             ): Map[K1,Map[K2,List[Int]]] =
  (mergeA.toList ++ mergeB.toList).groupMap(_._1)(_._2).map{
    case (k1,ms) =>
      k1 -> ms.flatMap(_.toList).groupMap(_._1)(_._2).map{
        case (k2,ls) => k2 -> ls.flatten
      }
    }

Non l'ho provato con mutableMaps ma dovrebbe funzionare più o meno allo stesso modo.