scanIndexed
所在位置是kotlin.collections.scanIndexed
,其相關用法介紹如下。
用法一
inline fun <T, R> Array<out T>.scanIndexed(
initial: R,
operation: (index: Int, acc: R, T) -> R
): List<R>
inline fun <R> ByteArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, Byte) -> R
): List<R>
inline fun <R> ShortArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, Short) -> R
): List<R>
inline fun <R> IntArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, Int) -> R
): List<R>
inline fun <R> LongArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, Long) -> R
): List<R>
inline fun <R> FloatArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, Float) -> R
): List<R>
inline fun <R> DoubleArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, Double) -> R
): List<R>
inline fun <R> BooleanArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, Boolean) -> R
): List<R>
inline fun <R> CharArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, Char) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UIntArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, UInt) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> ULongArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, ULong) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UByteArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, UByte) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UShortArray.scanIndexed(
initial: R,
operation: (index: Int, acc: R, UShort) -> R
): List<R>
返回一個列表,其中包含通過從左到右將 operation 應用於每個元素、其在原始數組中的索引和以 initial 值開頭的當前累加器值而生成的連續累加值。
請注意,傳遞給operation 函數的acc
值不應被改變;否則會影響結果列表中的前一個值。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.scan("s") { acc, string -> acc + string }) // [s, sa, sab, sabc, sabcd]
println(strings.scanIndexed("s") { index, acc, string -> acc + string + index }) // [s, sa0, sa0b1, sa0b1c2, sa0b1c2d3]
println(emptyList<String>().scan("s") { _, _ -> "X" }) // [s]
//sampleEnd
}
輸出:
[s, sa, sab, sabc, sabcd] [s, sa0, sa0b1, sa0b1c2, sa0b1c2d3] [s]
參數
用法二
inline fun <T, R> Iterable<T>.scanIndexed(
initial: R,
operation: (index: Int, acc: R, T) -> R
): List<R>
返回一個列表,其中包含通過從左到右將 operation 應用於每個元素、其在原始集合中的索引和以 initial 值開頭的當前累加器值而生成的連續累加值。
請注意,傳遞給operation 函數的acc
值不應被改變;否則會影響結果列表中的前一個值。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.scan("s") { acc, string -> acc + string }) // [s, sa, sab, sabc, sabcd]
println(strings.scanIndexed("s") { index, acc, string -> acc + string + index }) // [s, sa0, sa0b1, sa0b1c2, sa0b1c2d3]
println(emptyList<String>().scan("s") { _, _ -> "X" }) // [s]
//sampleEnd
}
輸出:
[s, sa, sab, sabc, sabcd] [s, sa0, sa0b1, sa0b1c2, sa0b1c2d3] [s]
參數
相關用法
- Kotlin scanIndexed用法及代碼示例
- Kotlin scan用法及代碼示例
- Kotlin sortedBy用法及代碼示例
- Kotlin splitToSequence用法及代碼示例
- Kotlin sort用法及代碼示例
- Kotlin setValue用法及代碼示例
- Kotlin sortWith用法及代碼示例
- 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用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.collections.scanIndexed。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。