是否可以在 Scala 中展平这个特定序列?
Is it possible to flatten this particular sequence in scala?
我有这样的结构:
Seq[(Int, mutable.Set[List[A]])]
我的目标是将其展平以获得类似 List[List[A]]
.
的结构
特别是,我的结构是这样的:
val order = List((1,HashSet(List('o'))), (4,HashSet(List('i', 'j', 'k', 'l'))), (3,HashSet(List('a', 'b', 'c'), List('f', 'g', 'h'))), (2,HashSet(List('d', 'e'), List('m', 'n'), List('z', 'x'))))
我想获得类似 val order = List( List('o'), List('i', 'j', 'k', 'l'), List('a', 'b', 'c'), ...)
的东西。
我试着用这种方式制作了一个map
:
order map {
case (_, Set[List[A]]) => List[A]
}
但它不起作用。错误是 "pattern type is incompatible with expected type"。
同样的事情也发生在:
case (Seq[(_, Set[List[A]])]) => List[A]
和 case (Seq[(_, mutable.Set[List[A]])]) => List[A]
.
我很确定要进行扁平化,解决方案是使用地图(或 flatMap),但显然我以错误的方式使用它。有人对我该怎么做有任何建议/想法吗?谢谢!
这个怎么样?
order.flatMap(_._2)
我有这样的结构:
Seq[(Int, mutable.Set[List[A]])]
我的目标是将其展平以获得类似 List[List[A]]
.
特别是,我的结构是这样的:
val order = List((1,HashSet(List('o'))), (4,HashSet(List('i', 'j', 'k', 'l'))), (3,HashSet(List('a', 'b', 'c'), List('f', 'g', 'h'))), (2,HashSet(List('d', 'e'), List('m', 'n'), List('z', 'x'))))
我想获得类似 val order = List( List('o'), List('i', 'j', 'k', 'l'), List('a', 'b', 'c'), ...)
的东西。
我试着用这种方式制作了一个map
:
order map {
case (_, Set[List[A]]) => List[A]
}
但它不起作用。错误是 "pattern type is incompatible with expected type"。
同样的事情也发生在:
case (Seq[(_, Set[List[A]])]) => List[A]
和 case (Seq[(_, mutable.Set[List[A]])]) => List[A]
.
我很确定要进行扁平化,解决方案是使用地图(或 flatMap),但显然我以错误的方式使用它。有人对我该怎么做有任何建议/想法吗?谢谢!
这个怎么样?
order.flatMap(_._2)