Say I have a class that can be nested many times: ...
# codereview
a
Say I have a class that can be nested many times:
Copy code
data class Node(val name: String, val children: List<Node> = emptyList())
and want to flatten it to a list. Is there any better way than something like:
Copy code
fun <T> Iterable<T>.flatten(transform: (T) -> Iterable<T>): List<T>
        = flatMap { listOf(it) + transform(it).flatten(transform) }
which can be used like
listOf(Node(...), ...).flatten { it.children }
?