runningReduceIndexed
所在位置是kotlin.collections.runningReduceIndexed
,其相關用法介紹如下。
用法一
inline fun <S, T : S> Array<out T>.runningReduceIndexed(
operation: (index: Int, acc: S, T) -> S
): List<S>
@ExperimentalUnsignedTypes inline fun UIntArray.runningReduceIndexed(
operation: (index: Int, acc: UInt, UInt) -> UInt
): List<UInt>
@ExperimentalUnsignedTypes inline fun ULongArray.runningReduceIndexed(
operation: (index: Int, acc: ULong, ULong) -> ULong
): List<ULong>
@ExperimentalUnsignedTypes inline fun UByteArray.runningReduceIndexed(
operation: (index: Int, acc: UByte, UByte) -> UByte
): List<UByte>
@ExperimentalUnsignedTypes inline fun UShortArray.runningReduceIndexed(
operation: (index: Int, acc: UShort, UShort) -> UShort
): List<UShort>
返回一個列表,其中包含通過從左到右將 operation 應用於每個元素、其在原始數組中的索引以及從該數組的第一個元素開始的當前累加器值而生成的連續累加值。
請注意,傳遞給operation 函數的acc
值不應被改變;否則會影響結果列表中的前一個值。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.runningReduce { acc, string -> acc + string }) // [a, ab, abc, abcd]
println(strings.runningReduceIndexed { index, acc, string -> acc + string + index }) // [a, ab1, ab1c2, ab1c2d3]
println(emptyList<String>().runningReduce { _, _ -> "X" }) // []
//sampleEnd
}
輸出:
[a, ab, abc, abcd] [a, ab1, ab1c2, ab1c2d3] []
參數
用法二
inline fun ByteArray.runningReduceIndexed(
operation: (index: Int, acc: Byte, Byte) -> Byte
): List<Byte>
inline fun ShortArray.runningReduceIndexed(
operation: (index: Int, acc: Short, Short) -> Short
): List<Short>
inline fun IntArray.runningReduceIndexed(
operation: (index: Int, acc: Int, Int) -> Int
): List<Int>
inline fun LongArray.runningReduceIndexed(
operation: (index: Int, acc: Long, Long) -> Long
): List<Long>
inline fun FloatArray.runningReduceIndexed(
operation: (index: Int, acc: Float, Float) -> Float
): List<Float>
inline fun DoubleArray.runningReduceIndexed(
operation: (index: Int, acc: Double, Double) -> Double
): List<Double>
inline fun BooleanArray.runningReduceIndexed(
operation: (index: Int, acc: Boolean, Boolean) -> Boolean
): List<Boolean>
inline fun CharArray.runningReduceIndexed(
operation: (index: Int, acc: Char, Char) -> Char
): List<Char>
返回一個列表,其中包含通過從左到右將 operation 應用於每個元素、其在原始數組中的索引以及從該數組的第一個元素開始的當前累加器值而生成的連續累加值。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.runningReduce { acc, string -> acc + string }) // [a, ab, abc, abcd]
println(strings.runningReduceIndexed { index, acc, string -> acc + string + index }) // [a, ab1, ab1c2, ab1c2d3]
println(emptyList<String>().runningReduce { _, _ -> "X" }) // []
//sampleEnd
}
輸出:
[a, ab, abc, abcd] [a, ab1, ab1c2, ab1c2d3] []
參數
用法三
inline fun <S, T : S> Iterable<T>.runningReduceIndexed(
operation: (index: Int, acc: S, T) -> S
): List<S>
返回一個列表,其中包含通過從左到右將 operation 應用於每個元素、其在原始集合中的索引以及從該集合的第一個元素開始的當前累加器值而生成的連續累加值。
請注意,傳遞給operation 函數的acc
值不應被改變;否則會影響結果列表中的前一個值。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.runningReduce { acc, string -> acc + string }) // [a, ab, abc, abcd]
println(strings.runningReduceIndexed { index, acc, string -> acc + string + index }) // [a, ab1, ab1c2, ab1c2d3]
println(emptyList<String>().runningReduce { _, _ -> "X" }) // []
//sampleEnd
}
輸出:
[a, ab, abc, abcd] [a, ab1, ab1c2, ab1c2d3] []
參數
相關用法
- Kotlin runningReduceIndexed用法及代碼示例
- Kotlin runningReduce用法及代碼示例
- Kotlin runningFoldIndexed用法及代碼示例
- Kotlin runningFold用法及代碼示例
- Kotlin reduceRight用法及代碼示例
- Kotlin reduceIndexed用法及代碼示例
- Kotlin reduceTo用法及代碼示例
- Kotlin reversed用法及代碼示例
- Kotlin reduceRightIndexed用法及代碼示例
- Kotlin replaceFirstChar用法及代碼示例
- Kotlin reduceIndexedOrNull用法及代碼示例
- Kotlin rangeTo用法及代碼示例
- Kotlin reduce用法及代碼示例
- Kotlin replace用法及代碼示例
- Kotlin reduceRightIndexedOrNull用法及代碼示例
- Kotlin reduceRightOrNull用法及代碼示例
- Kotlin reduceOrNull用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin map用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin Random.Default用法及代碼示例
- Kotlin Byte.inc用法及代碼示例
- Kotlin getValue用法及代碼示例
- Kotlin Double.dec用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.collections.runningReduceIndexed。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。