W3cubDocs

/Kotlin

zip

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
infix fun <T, R> Sequence<T>.zip(
    other: Sequence<R>
): Sequence<Pair<T, R>>

Returns a sequence of values built from the elements of this sequence and the other sequence with the same index. The resulting sequence ends as soon as the shortest input sequence ends.

The operation is intermediate and stateless.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val sequenceA = ('a'..'z').asSequence()
val sequenceB = generateSequence(1) { it * 2 + 1 }

println((sequenceA zip sequenceB).take(4).toList()) // [(a, 1), (b, 3), (c, 7), (d, 15)]
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T, R, V> Sequence<T>.zip(
    other: Sequence<R>, 
    transform: (a: T, b: R) -> V
): Sequence<V>

Returns a sequence of values built from the elements of this sequence and the other sequence with the same index using the provided transform function applied to each pair of elements. The resulting sequence ends as soon as the shortest input sequence ends.

The operation is intermediate and stateless.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val sequenceA = ('a'..'z').asSequence()
val sequenceB = generateSequence(1) { it * 2 + 1 }

val result = sequenceA.zip(sequenceB) { a, b -> "$a/$b" }
println(result.take(4).toList()) // [a/1, b/3, c/7, d/15]
//sampleEnd
}

© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/zip.html