August 2017
Intermediate to advanced
440 pages
10h
English
Sometimes, the element processing method depends on its index in the list. The most universal way to solve this problem is by using the withIndex function, which returns a list of values with indexes:
listOf(9,8,7,6).withIndex() // 1
.filter { (i, _) -> i % 2 == 0 } // 2
.forEach { (i, v) -> print("$v at $i,") }
// Prints: 9 at 0, 7 at 2,
Also, there are variants ...
Read now
Unlock full access