Scala:Map.flatten 的用例?
Scala: Use case for Map.flatten?
documentation on Map.flatten 声明如下:
Converts this map of traversable collections into a map formed by the elements of these traversable collections.
我得到 "map of traversable collections." 例如,这将是一张列表地图。仅根据该定义,Map[Int, List[Int]]
就符合条件。
但是 "a map formed by the elements of these traversable collections" 是什么?听起来很简单,但我很难让它发挥作用。
文档中提供的示例代码是......好吧......我们应该说,不适用吗?
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)
我尝试了几种不同的方法,但它们都产生了相同的错误。这里有几个例子:
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
^
我错过了什么?
问题是编译器不"know"如何解释您存储在地图中的元素。也就是说,解释不明显,因此您必须将自己对元素的隐式视图提供到可遍历中。例如,对于您提供的情况,您希望将 (Int, List[Int])
类型映射的每个元素解释为一个新的元组列表,其中第一个元素是原始元素键,值是每个值最初在给定键的值中。在代码中:
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
但是你必须自己提供"flattening"功能。
documentation on Map.flatten 声明如下:
Converts this map of traversable collections into a map formed by the elements of these traversable collections.
我得到 "map of traversable collections." 例如,这将是一张列表地图。仅根据该定义,Map[Int, List[Int]]
就符合条件。
但是 "a map formed by the elements of these traversable collections" 是什么?听起来很简单,但我很难让它发挥作用。
文档中提供的示例代码是......好吧......我们应该说,不适用吗?
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)
我尝试了几种不同的方法,但它们都产生了相同的错误。这里有几个例子:
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
^
我错过了什么?
问题是编译器不"know"如何解释您存储在地图中的元素。也就是说,解释不明显,因此您必须将自己对元素的隐式视图提供到可遍历中。例如,对于您提供的情况,您希望将 (Int, List[Int])
类型映射的每个元素解释为一个新的元组列表,其中第一个元素是原始元素键,值是每个值最初在给定键的值中。在代码中:
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
但是你必须自己提供"flattening"功能。