Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: Use case for Map.flatten?

The documentation on Map.flatten states the following:

Converts this map of traversable collections into a map formed by the elements of these traversable collections.

I get "map of traversable collections." That would be a map of lists, for example. By that definition alone, a Map[Int, List[Int]] would qualify.

But what is "a map formed by the elements of these traversable collections"? It sounds straightforward, but I'm having a hard time getting it to work.

The example code provided in the documentation is ... well ... shall we say, not applicable?

val xs = List(
           Set(1, 2, 3),
           Set(1, 2, 3)
         ).flatten
// xs == List(1, 2, 3, 1, 2, 3)

val ys = Set(
           List(1, 2, 3),
           List(3, 2, 1)
         ).flatten
// ys == Set(1, 2, 3)

I've tried a few different things, but they yield the same error. Here are a couple of examples:

scala> val m = Map(List(1) -> List(1,2,3), List(2) -> List(4,5,6), List(3) -> List(7,8,9))
m: scala.collection.immutable.Map[List[Int],List[Int]] = Map(List(1) -> List(1, 2, 3), List(2) -> List(4, 5, 6), List(3) -> List(7, 8, 9))

scala> m.flatten
<console>:9: error: No implicit view available from (List[Int], List[Int]) => scala.collection.GenTraversableOnce[B].
              m.flatten
                ^

scala> val m = Map(1 -> List(1,2,3), 2 -> List(4,5,6), 4 -> List(7,8,9))
m: scala.collection.immutable.Map[Int,List[Int]] = Map(1 -> List(1, 2, 3), 2 -> List(4, 5, 6), 4 -> List(7, 8, 9))

scala> m.flatten
<console>:9: error: No implicit view available from (Int, List[Int]) => scala.collection.GenTraversableOnce[B].
              m.flatten
                ^

What am I missing?

like image 437
Brad Collins Avatar asked Apr 11 '15 13:04

Brad Collins


People also ask

What is flat map in Scala?

In Scala, flatMap() method is identical to the map() method, but the only difference is that in flatMap the inner grouping of an item is removed and a sequence is generated. It can be defined as a blend of map method and flatten method.

How does flatten work in Scala?

The flatten function is applicable to both Scala's Mutable and Immutable collection data structures. The flatten method will collapse the elements of a collection to create a single collection with elements of the same type.

What is difference between MAP and HashMap in Scala?

HashMap is an implementation of Map . As you can see in their definitions HashMap is a class and Map is a trait. In Programming in Scala, 1ed (search for "Default immutable map implementations") they write that a HashMap is the default implementation for a Map with 5 elements or more.

How is map implemented in Scala?

Per the Scaladoc, “implements immutable maps using a list-based data structure.” As shown in the examples, elements that are added are prepended to the head of the list. Keys of the map are returned in sorted order. Therefore, all traversal methods (such as foreach) return keys in that order.


1 Answers

The problem is that the compiler doesn't "know" how to interpret the elements that you store in the map. That is, the interpretation is not evident, so you have to provide your own implicit view of the elements into a traversable. For example, for the case you provide, you want to interpret each element of the map of type (Int, List[Int]) perhaps into a new list of tuples in which the first element is the original element key and the value is each of the values originally in the given key's value. In code:

implicit val flattener = (t: (Int,List[Int])) ⇒ t._2.map(x ⇒ (t._1, x))

val m = Map(1 → List(1, 2, 3), 2 → List(4, 5), 3 → List(6))
val fm = m.flatten

But you have to provide the "flattening" function yourself.

like image 149
ale64bit Avatar answered Oct 04 '22 21:10

ale64bit