// Infinite Loop Using generateSequence (Functional style)
fun main() {
var count = 0
/*
enerateSequence(0) { it + 1 } creates an infinite sequence starting at 0.
The seed (first value) is 0
The next value is computed by the lambda:
it + 1
So the sequence becomes: 0, 1, 2, 3, 4, 5, 6, ...
It never ends because there is no stopping condition.
*/
generateSequence(0) { it + 1 }.forEach { // infinite loop
println("Index: $count")
if (count == 3) {
println("Breaking out of loop...")
return
}
count++
}
}
/*
run:
Index: 0
Index: 1
Index: 2
Index: 3
Breaking out of loop...
*/