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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。