fun <T> Sequence<T>.asSequence(): Sequence<T>
Returns this sequence as a Sequence.
fun <T> Iterator<T>.asSequence(): Sequence<T>
Creates a sequence that returns all elements from this iterator. The sequence is constrained to be iterated only once.
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/as-sequence.html