如何在Scala中展平List [Any,List []]

时间:2018-04-14 11:28:01

标签: scala

val xs = List(List(4, List(4, 7)), List(3, List(3, 5)), List(4, List(5, 9))

如何将其转换为:

List(List(4, 4, 7), List(3, 3, 5), List(4, 5, 9))

1 个答案:

答案 0 :(得分:0)

这样的东西?

xs.map(arr => arr.foldLeft(List[Any]())((acc, value) => value match {
    case xs:List[Any] => acc ++ xs
    case x:Any => acc :+ x
}))