Spec-Zone.ru › Kotlin 1.7

Iterable

kotlin-stdlib / kotlin.collections / Iterable
Требования к платформе и версии: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T> Iterable(
    crossinline iterator: () -> Iterator<T>
): Iterable<T>

На основе функции iterator создаётся экземпляр Iterable, возвращающий значения через предоставленный функцией Iterator.



fun main(args: Array<String>) {
//sampleStart
val iterable = Iterable {
    iterator {
        yield(42)
        yieldAll(1..5 step 2)
    }
}
val result = iterable.mapIndexed { index, value -> "$index: $value" }
println(result) // [0: 42, 1: 1, 2: 3, 3: 5]

// can be iterated many times
repeat(2) {
    val sum = iterable.sum()
    println(sum) // 51
}
//sampleEnd
}

© 2010–2022 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/-iterable.html

Spec-Zone.ru

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