Lilly
11/02/2020, 4:59 PMarrayOf(
Libraries.APPCOMPAT,
*Libraries.KotlinExt.getAll(),
Libraries.TIMBER
).forEach { // do sth. }
getAll()
returns an array but could als be another type of collection, I'm flexible here. What would be an idomatic solution?Nir
11/02/2020, 5:01 PMNir
11/02/2020, 5:02 PMLibraries.KotlinExt.getAll() + Libraries.APPCOMPAT + Libraries.TIMBER
though that's significantly less efficientNir
11/02/2020, 5:02 PMLilly
11/02/2020, 5:03 PMCasey Brooks
11/02/2020, 5:14 PM.flatten()
, use a sequence, etc.
listOf(
listOf(Libraries.APPCOMPAT),
Libraries.KotlinExt.getAll(),
listOf(Libraries.TIMBER)
).flatten().forEach { ... }
or
sequence {
yield(Libraries.APPCOMPAT)
yieldAll(Libraries.KotlinExt.getAll())
listOf(Libraries.TIMBER)
}.forEach { ... }
Though from the example you posted, it’s probably going to have more overhead to both of those than just using the spread operator. Spread is perfectly idiomatic Kotlin code, there’s no reason not to use it when it solves the problem effectively and you aren’t dealing with huge sets of data (on the order of thousands of elements)Lilly
11/02/2020, 5:46 PMCasey Brooks
11/02/2020, 5:49 PM+
operator is still copying elements from those lists into a single bigger one at runtime.Nir
11/02/2020, 5:52 PMNir
11/02/2020, 5:52 PMCasey Brooks
11/02/2020, 5:53 PMNir
11/02/2020, 5:53 PMNir
11/02/2020, 5:53 PMNir
11/02/2020, 5:53 PMLilly
11/02/2020, 6:47 PM