W3cubDocs

/Kotlin

zipWithNext

Platform and version requirements: JVM (1.2), JS (1.2), Native (1.2)
fun CharSequence.zipWithNext(): List<Pair<Char, Char>>

Returns a list of pairs of each two adjacent characters in this char sequence.

The returned list is empty if this char sequence contains less than two characters.

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
}
Platform and version requirements: JVM (1.2), JS (1.2), Native (1.2)
inline fun <R> CharSequence.zipWithNext(
    transform: (a: Char, b: Char) -> R
): List<R>

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

The returned list is empty if this char sequence contains less than two characters.

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–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/zip-with-next.html