public inline fun <T, R> Iterable<T>.flatMap(transform: (T) -> Iterable<R>): List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
public inline fun <T, R, C : MutableCollection<in R>>
Iterable<T>.flatMapTo(destination: C, transform: (T) -> Iterable<R>): C {
for (element in this) {
val list = transform(element)
destination.addAll(list)
}
return destination
}
val list = listOf(1, 2, 3, 4, 5)
println(list.flatMap{ listOf(it, it * 2, it * 3) })
// [1, 2, 3, 2, 4, 6, 3, 6, 9, 4, 8, 12, 5, 10, 15]