W3cubDocs

/Kotlin

zipWithNext

fun <T> Sequence<T>.zipWithNext(): Sequence<Pair<T, T>>

Platform and version requirements: Kotlin 1.2

Returns a sequence of pairs of each two adjacent elements in this sequence.

The returned sequence is empty if this sequence contains less than two elements.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val letters = ('a'..'f').toList()
val pairs = letters.zipWithNext()

println(letters) // [a, b, c, d, e, f]
println(pairs) // [(a, b), (b, c), (c, d), (d, e), (e, f)]
//sampleEnd
}
fun <T, R> Sequence<T>.zipWithNext(
    transform: (a: T, b: T) -> R
): Sequence<R>

Platform and version requirements: Kotlin 1.2

Returns a sequence containing the results of applying the given transform function to an each pair of two adjacent elements in this sequence.

The returned sequence is empty if this sequence contains less than two elements.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val values = listOf(1, 4, 9, 16, 25, 36)
val deltas = values.zipWithNext { a, b -> b - a }

println(deltas) // [3, 5, 7, 9, 11]
//sampleEnd
}

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