Say I have a list of integers, as such:
val list = listOf(1, 2, 3, 5, 8, 13)
I want to run an operation to create a similar list but with an additional element after every element. For example, we should be able to add a -99 after each element as such:
[1, -99, 2, -99, 3, -99, 5, -99, 8, -99, 13, -99]
I can do this with a forEach loop and temporary list easily, as such:
val list = listOf(1, 2, 3, 5, 8, 13)
val tempList = mutableListOf<Int>()
list.forEach {
tempList.add(it)
tempList.add(-99)
}
println(tempList) // [1, -99, 2, -99, 3, -99, 5, -99, 8, -99, 13, -99]
But this seems unnecessarily verbose and imperative. Another way I found is to map each item to a list with the two elements, creating a list of lists, and flattening the result. Example:
val list = listOf(1, 2, 3, 5, 8, 13)
val mapFlatten = list
.map { listOf(it, -99) }
.flatten()
println(mapFlatten) // [1, -99, 2, -99, 3, -99, 5, -99, 8, -99, 13, -99]
However, creating a list of lists and then flattening seems unnecessarily redundant. Is there any list operation that allows us to do this in a simpler way?