Title
a

aballano

03/18/2019, 1:35 PM
hi there, small question: having 2 lists such as
``[1, 2, 3]``
and
``[4, 5, 6, 7]``
is there an existing function that intercalates both so the result is
``[1, 4, 2, 5, 3, 6, 7]``
?
I just came up with this, but wondering if there's something already built-in
``````fun <E> List<E>.merge(other: List<E>): List<E> {
val out = ArrayList<E>(size + other.size)
forEachIndexed { index, e ->
}
return out
}``````
d

diesieben07

03/18/2019, 1:40 PM
``la.zip(lb) { a, b -> listOf(a, b) }.flatten()``
almost works, in that it only keeps the length of the shortest list
w

wbertan

03/18/2019, 1:40 PM
Check if this suits you:
``````@Test
fun asas1() {
val l1 = listOf(1, 2, 3)
val l2 = listOf(4, 5, 6, 7)
val expected = listOf(1, 4, 2, 5, 3, 6, 7)

val result =
l1.zip(l2).flatMap { listOf(it.first, it.second) } +
l1.drop(l2.size) + l2.drop(l1.size)

assertEquals(expected, result)
}

@Test
fun asas2() {
val l1 = listOf(4, 5, 6, 7)
val l2 = listOf(1, 2, 3)
val expected = listOf(4, 1, 5, 2, 6, 3, 7)

val result =
l1.zip(l2).flatMap { listOf(it.first, it.second) } +
l1.drop(l2.size) + l2.drop(l1.size)

assertEquals(expected, result)
}``````
Can have this extension:
``````fun <T> List<T>.mix(other: List<T>) =
this.zip(other).flatMap { listOf(it.first, it.second) } +
this.drop(other.size) + other.drop(this.size)``````
r

ribesg

03/18/2019, 1:46 PM
Most of those solutions are very inefficient, hopefully we’re not talking about huge lists here
a

aballano

03/18/2019, 2:42 PM
Ok, I see, thanks @wbertan! I think I'll keep with my function for now
👍 1
p

Pavlo Liapota

03/18/2019, 3:08 PM
@aballano Note, that your function returns
``[1, 4, 2, 5, 3, 6]``
``[1, 4, 2, 5, 3, 6, 7]``
👍 1
r

ribesg

03/18/2019, 3:27 PM
I would use something more like this (untested)
``````fun <T> Iterable<T>.alternateWith(other: Iterable<T>): Iterable<T> =
Iterable {
object : Iterator<T> {

private val a = this@alternateWith.iterator()
private val b = other.iterator()
private var switch = false

override fun hasNext(): Boolean =
a.hasNext() || b.hasNext()

override fun next(): T {
switch = !switch
return if (switch) next(b, a) else next(a, b)
}

private fun next(first: Iterator<T>, second: Iterator<T>): T =
if (first.hasNext()) first.next() else second.next()

}
}``````