Reading all the elements in the sequence

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:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.239.166