inline fun <T> Sequence( crossinline iterator: () -> Iterator<T> ): Sequence<T>
Given an iterator function constructs a Sequence that returns values through the Iterator provided by that function. The values are evaluated lazily, and the sequence is potentially infinite.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val array = arrayOf(1, 2, 3)
// create a sequence with a function, returning an iterator
val sequence1 = Sequence { array.iterator() }
println(sequence1.joinToString()) // 1, 2, 3
println(sequence1.drop(1).joinToString()) // 2, 3
// create a sequence from an existing iterator
// can be iterated only once
val sequence2 = array.iterator().asSequence()
println(sequence2.joinToString()) // 1, 2, 3
// sequence2.drop(1).joinToString() // <- iterating sequence second time will fail
//sampleEnd
}
© 2010–2020 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.sequences/-sequence.html