windowed
所在位置是kotlin.collections.windowed
,其相關用法介紹如下。
用法一
fun <T> Iterable<T>.windowed(
size: Int,
step: Int = 1,
partialWindows: Boolean = false
): List<List<T>>
返回給定 size 窗口的快照列表,沿此集合滑動給定 step ,其中每個快照都是一個列表。
最後幾個列表的元素可能比給定的 size 少。
size 和 step 都必須為正數,並且可以大於此集合中的元素數。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val sequence = generateSequence(1) { it + 1 }
val windows = sequence.windowed(size = 5, step = 1)
println(windows.take(4).toList()) // [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7], [4, 5, 6, 7, 8]]
val moreSparseWindows = sequence.windowed(size = 5, step = 3)
println(moreSparseWindows.take(4).toList()) // [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [10, 11, 12, 13, 14]]
val fullWindows = sequence.take(10).windowed(size = 5, step = 3)
println(fullWindows.toList()) // [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8]]
val partialWindows = sequence.take(10).windowed(size = 5, step = 3, partialWindows = true)
println(partialWindows.toList()) // [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10], [10]]
//sampleEnd
}
輸出:
[[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7], [4, 5, 6, 7, 8]] [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [10, 11, 12, 13, 14]] [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8]] [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10], [10]]
參數
用法二
fun <T, R> Iterable<T>.windowed(
size: Int,
step: Int = 1,
partialWindows: Boolean = false,
transform: (List<T>) -> R
): List<R>
返回將給定 transform 函數應用於每個列表的結果列表,該列表表示給定 size 窗口上的視圖,使用給定 step 沿著此集合滑動。
請注意,傳遞給transform 函數的列表是短暫的,僅在該函數內部有效。您不應該存儲它或讓它以某種方式逃逸,除非您製作了它的快照。最後幾個列表的元素可能比給定的 size 少。
size 和 step 都必須為正數,並且可以大於此集合中的元素數。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val dataPoints = sequenceOf(10, 15, 18, 25, 19, 21, 14, 8, 5)
val averaged = dataPoints.windowed(size = 4, step = 1, partialWindows = true) { window -> window.average() }
println(averaged.toList()) // [17.0, 19.25, 20.75, 19.75, 15.5, 12.0, 9.0, 6.5, 5.0]
val averagedNoPartialWindows = dataPoints.windowed(size = 4, step = 1).map { it.average() }
println(averagedNoPartialWindows.toList()) // [17.0, 19.25, 20.75, 19.75, 15.5, 12.0]
//sampleEnd
}
輸出:
[17.0, 19.25, 20.75, 19.75, 15.5, 12.0, 9.0, 6.5, 5.0] [17.0, 19.25, 20.75, 19.75, 15.5, 12.0]
參數
相關用法
- Kotlin windowedSequence用法及代碼示例
- Kotlin windowed用法及代碼示例
- Kotlin withIndex用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin map用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin reduceRight用法及代碼示例
- Kotlin Random.Default用法及代碼示例
- Kotlin Byte.inc用法及代碼示例
- Kotlin getValue用法及代碼示例
- Kotlin Double.dec用法及代碼示例
- 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 indexOf用法及代碼示例
- Kotlin reduceIndexed用法及代碼示例
- Kotlin filterKeys用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.collections.windowed。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。