Simon Lin
11/12/2021, 7:58 AMval deepList = listOf(
listOf(
listOf(-1, 0),
listOf(1)
),
listOf(2, 3),
listOf(4, 5, 6)
)
println(deepList.flatten()) // [-1, 0, 1, 2, 3, 4, 5, 6]
ex2:
val deepList = listOf(
-1,
listOf(listOf(listOf(0))),
listOf(2, 3),
listOf(4, 5, 6)
)
println(deepList.flatten()) // [-1, 0, 1, 2, 3, 4, 5, 6]Tobias Suchalla
11/12/2021, 8:12 AMfun main() {
val deepList = listOf(
listOf(
listOf(-1, 0),
listOf(1)
),
listOf(2, 3),
listOf(4, 5, 6)
)
println(deepList.deepFlatten())
}
fun Iterable<*>.deepFlatten(): Iterable<*> = flatMap {
when (it) {
is Iterable<*> -> it.deepFlatten()
else -> listOf(it)
}
}
(Quickly cobbled together, not necessarily the best approch. YMMV)Ulrik Rasmussen
11/12/2021, 11:21 AMIterable will silently get flattened. It also loses the type argument. I'd recommend not having heterogeneous lists like that if that can be avoided.Ulrik Rasmussen
11/12/2021, 11:28 AMIterable, but I doubt you would want listOf(mapOf("foo" to 42)).deepFlatten() to give you listOf(Pair("foo", 42)).Tobias Suchalla
11/12/2021, 12:11 PMList<Any>, so there is no type to begin with.
Regarding the use of Iterable, it depends on one's needs. You are right, however. To be safe, one might want seperate functions for list and set flattening.
However, Map does not implement Iterable 🤨
val deepList = listOf(mapOf("foo" to 42)).deepFlatten()
println(deepList.deepFlatten())
// [{foo=42}]Ulrik Rasmussen
11/12/2021, 12:14 PMUlrik Rasmussen
11/12/2021, 12:15 PMIterable is implemented by a lot of things.Tobias Suchalla
11/12/2021, 12:19 PM