July 2018
Intermediate to advanced
266 pages
7h 11m
English
At the beginning of this section, we mentioned that suspending sequences are stateless and that they reset after being used. Consider the following sequence:
val sequence = buildSequence { for (i in 0..9) { println("Yielding $i") yield(i) }}
It's a simple sequence that can yield up to 10 values. Now, we might read the values like this:
fun main(args: Array<String>) { println("Requesting index 1") sequence.elementAt(1) println("Requesting index 2") sequence.elementAt(2) println("Taking 3") sequence.take(3).joinToString()}
You may notice that a sequence will simply execute from the beginning for each call, which is different from using an iterator:
Read now
Unlock full access