Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cats.kernel.Semigroup
- def mergeMap[K, V](left: Map[K, V], right: Map[K, V]): Map[K, List[V]] = {
- import cats.implicits._
- def optCombine[A: Semigroup](a: A, optB: Option[A]): A = optB.map(b => if (a == b) a else a |+| b).getOrElse(a)
- def merge[K, V : Semigroup](left: Map[K, V], right: Map[K, V]): Map[K, V] = {
- left.foldLeft(right) { case (acc, (k, v)) => acc.updated(k, optCombine(v, acc.get(k)))}
- }
- val l = left.map{ case (k, v) => k -> List(v)}
- val r = right.map{ case (k, v) => k -> List(v)}
- merge(l, r)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement