Using the 'flatMap' Function for Flattening Nested Collections

The 'flatMap' function is useful when you have a collection of collections and you want to transform each element and flatten the results into a single collection. This can be particularly handy when dealing with lists of lists, such as when you have data structures that represent a hierarchy or nested relationships.

val nestedList = listOf(
    listOf(1, 2, 3),
    listOf(4, 5),
    listOf(6, 7, 8, 9)
)

val flattenedList = nestedList.flatMap { it.map { number -> number * 2 } }
// flattenedList will contain [2, 4, 6, 8, 10, 12, 14, 16, 18]