zip

두개의 리스트를 합쳐 pair 의 리스트를 반환한다

public inline fun <T, R, V> Iterable<T>.zip(other: Iterable<R>, transform:
(a: T, b: R) -> V): List<V> {
        val first = iterator()
        val second = other.iterator()
        val list = ArrayList<V>(
        minOf(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))
        )
        while (first.hasNext() && second.hasNext()) {
                list.add(transform(first.next(), second.next()))
        }
        return list
}


val list = listOf(1, 2, 3, 4, 5)
val list2 = listOf(1, 2, 3, 4, 5)

println(list.zip(list2))  // [(1,1), (2,2), (3,3), (4,4), (5,5)]

val list3 = listOf(1, 2, 3)
println(list.zip(list3))  // [(1,1), (2,2), (3,3)] 
// 각 리스트의 길이가 다를때는 짧은 쪽에 맞춘

Last updated