scalascala-collectionsscala-genericsscala-implicits

Generically extending Scala collections


I have a Seq[(A, B)]. I wanted to add an implicit method to such collections so I can do .toMultiMap to get back a Map[A, Seq[B]].

This was my first attempt:

  implicit class PairsExtensions[A, B](t: Traversable[(A, B)]) {
    def toMultiMap: Map[A, Traversable[B]] = t.groupBy(_._1).mapValues(_.map(_._2))
  }

But, now the problem is I always get back a Traversable for the values. I want to get back a Map[A, Set[B]] if I do Set[(A, B)].toMultiMap.

So, then I tried something like this:

 implicit class PairsExtensions2[A, B, Repr[_] <: Traversable[(A, B)]](t: TraversableLike[(A, B), Repr[(A, B)]]) {
    def toMultiMap(implicit bf: CanBuild[B, Repr[B]]): Map[A, Repr[B]] = t.groupBy(_._1).mapValues(_.map(_._2))
  }

But, it does not work:

val m1: Map[Int, Set[String]] = Set.empty[(Int, String)]
val m2: Map[Int, List[String]] = List.empty[(Int, String)]

What is the way to do this?


Solution

  • I think this might be what you're after.

    import scala.collection.generic.CanBuildFrom
    import scala.language.higherKinds
    
    implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
      def toMultiMap(implicit cbf: CanBuildFrom[Nothing, B, C[B]]): Map[A, C[B]] =
        t.groupBy(_._1).mapValues(_.map(_._2).to[C])
    }
    

    This passes a few simple tests:

    scala> val m1: Map[Int, Set[String]] = Set.empty[(Int, String)].toMultiMap
    m1: Map[Int,Set[String]] = Map()
    
    scala> val m2: Map[Int, List[String]] = List.empty[(Int, String)].toMultiMap
    m2: Map[Int,List[String]] = Map()
    
    scala> Seq(('c',4),('x',2),('c',5)).toMultiMap
    res4: Map[Char,Seq[Int]] = Map(x -> Vector(2), c -> Vector(4, 5))