zipWithNext所在位置是kotlin.text.zipWithNext,其相關用法介紹如下。

用法一

fun CharSequence.zipWithNext(): List<Pair<Char, Char>>

返回此 char 序列中每兩個相鄰字符的對列表。

如果此 char 序列包含少於兩個字符,則返回的列表為空。

例子:

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
}

輸出:

[a, b, c, d, e, f]
[(a, b), (b, c), (c, d), (d, e), (e, f)]

用法二

inline fun <R> CharSequence.zipWithNext(
    transform: (a: Char, b: Char) -> R
): List<R>

返回一個列表,其中包含將給定的 transform 函數應用於此字符序列中的每對兩個相鄰字符的結果。

如果此 char 序列包含少於兩個字符,則返回的列表為空。

例子:

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
}

輸出:

[3, 5, 7, 9, 11]