Given a complex object like the following:
case class Complex
(
id: Long,
name: String,
nested: Seq[Complex]
)
In action this can turn into something like this:
val stuff =
List(
Complex(1, "name1",
List(
Complex(2, "name2", List()),
Complex(3, "name3",
List(
Complex(4, "name4", List())
)
)
)
)
)
I need to turn it into a flat list of Complex
objects, pulling all the children/grandchildren up.
val flattened =
List(
Complex(1, "name1", List()),
Complex(2, "name2", List()),
Complex(3, "name3", List()),
Complex(4, "name4", List()),
)
Do you have any leads/ideas on how I can accomplish this?
The other solutions I have tried seem to only do simple nesting of lists. Things I've tried:
- How does this recursive List flattening work?
- Generic, type-safe way to flatten arbitrarily nested collections in Scala?
These all seem to yield the same list I started with.
flat
in this page has grown to 32. :-) – stefanobaghino