To go through all the elements in the sequence, you can use forEach() and forEachIndexed(). Both of them will behave similarly, but forEachIndexed() is an extension function that provides the index of the value along with the value itself:
sequence.forEach {
print("$it ")
}
sequence.forEachIndexed { index, value ->
println("element at $index is $value")
}
The first of the previous snippets will print the eight numbers in the sequence: