clubfan
03/30/2018, 12:41 PMDaniel
03/30/2018, 12:43 PMclubfan
03/30/2018, 12:46 PMlistOf("a", "b", "c").zip(listOf("d", "e")).flatMap(it.toList())
maybe? Haven't tested it thoughDaniel
03/30/2018, 12:48 PM<[a, d, b, e]>
list1.zip(list2).flatMap { it.toList() } + list1.takeLast(1)
would do itclubfan
03/30/2018, 1:05 PMfun <T> List<T>.merge(other : List<T>) : List<T> {
val result = ArrayList<T>()
for ( i in 0 .. maxOf(this.size, other.size)) {
this.getOrNull(i)?.let { result.add(it) }
other.getOrNull(i)?.let { result.add(it) }
}
return result
}
works, but it's not really idiomatic I thinklistOf("a", "b", "c").merge(listOf("d", "e"))
fun <T> List<T>.merge(other : List<T>) : List<T> {
return this.mapIndexed{i, el -> other.getOrNull(i)?.let { listOf(el, it) } ?: listOf(el)}.flatten() + other.drop(this.size)
}
ilya.gorbunov
03/30/2018, 2:57 PMnull
in zipAll
means: either null last element or missing last element.Daniel
03/30/2018, 9:44 PMrequire(other.size == size - 1)
function beforehand!