Spec-Zone.ru › Kotlin 1.8

scan

kotlin-stdlib / kotlin.collections / scan
Требования к платформе и версии: JVM (1.4), JS (1.4), Native (1.4)
inline fun <T, R> Array<out T>.scan(
    initial: R, 
    operation: (acc: R, T) -> R
): List<R>
inline fun <R> ByteArray.scan(
    initial: R, 
    operation: (acc: R, Byte) -> R
): List<R>
inline fun <R> ShortArray.scan(
    initial: R, 
    operation: (acc: R, Short) -> R
): List<R>
inline fun <R> IntArray.scan(
    initial: R, 
    operation: (acc: R, Int) -> R
): List<R>
inline fun <R> LongArray.scan(
    initial: R, 
    operation: (acc: R, Long) -> R
): List<R>
inline fun <R> FloatArray.scan(
    initial: R, 
    operation: (acc: R, Float) -> R
): List<R>
inline fun <R> DoubleArray.scan(
    initial: R, 
    operation: (acc: R, Double) -> R
): List<R>
inline fun <R> BooleanArray.scan(
    initial: R, 
    operation: (acc: R, Boolean) -> R
): List<R>
inline fun <R> CharArray.scan(
    initial: R, 
    operation: (acc: R, Char) -> R
): List<R>
inline fun <T, R> Iterable<T>.scan(
    initial: R, 
    operation: (acc: R, T) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UIntArray.scan(
    initial: R, 
    operation: (acc: R, UInt) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> ULongArray.scan(
    initial: R, 
    operation: (acc: R, ULong) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UByteArray.scan(
    initial: R, 
    operation: (acc: R, UByte) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UShortArray.scan(
    initial: R, 
    operation: (acc: R, UShort) -> R
): List<R>

Возвращает список, содержащий последовательные значения накопления, полученные путём применения operation слева направо к каждому элементу и текущему значению накопителя, начинающемуся со значения initial.

Обратите внимание, что acc значение, переданное в функцию operation, не должно изменяться; в противном случае это повлияет на предыдущее значение в результирующем списке.

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
}

Параметры

operation - функция, которая принимает текущее значение накопителя и элемент и вычисляет следующее значение накопителя.

© 2010–2023 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/scan.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API