zipWithNext
所在位置是kotlin.sequences.zipWithNext
,其相關用法介紹如下。
用法一
返回此序列中每兩個相鄰元素的對序列。
如果此序列包含少於兩個元素,則返回的序列為空。
操作是 intermediate
和 stateless
。
例子:
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)]
用法二
返回一個序列,其中包含將給定的transform 函數應用於該序列中每對兩個相鄰元素的結果。
如果此序列包含少於兩個元素,則返回的序列為空。
操作是 intermediate
和 stateless
。
例子:
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]
相關用法
- Kotlin zipWithNext用法及代碼示例
- Kotlin zip用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin map用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin reduceRight用法及代碼示例
- Kotlin Random.Default用法及代碼示例
- Kotlin Byte.inc用法及代碼示例
- Kotlin getValue用法及代碼示例
- Kotlin Double.dec用法及代碼示例
- Kotlin windowedSequence用法及代碼示例
- Kotlin contentToString用法及代碼示例
- Kotlin groupByTo用法及代碼示例
- Kotlin commonPrefixWith用法及代碼示例
- Kotlin MatchResult.Destructured用法及代碼示例
- Kotlin Delegates.notNull用法及代碼示例
- Kotlin ifBlank用法及代碼示例
- Kotlin filterNotTo用法及代碼示例
- Kotlin getOrPut用法及代碼示例
- Kotlin Triple.<init>用法及代碼示例
- Kotlin Duration.toString用法及代碼示例
- Kotlin windowed用法及代碼示例
- Kotlin indexOf用法及代碼示例
- Kotlin reduceIndexed用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.sequences.zipWithNext。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。