Functional constructs for iteration
suggest changeThe Kotlin Standard Library also provides numerous useful functions to iteratively work upon collections.
For example, the map function can be used to transform a list of items.
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
val numberStrings = numbers.map { "Number $it" }
One advantage of this style is that operations can be chained. Only a small change is required if, say, the list above should be filtered to even numbers. The filter function does that.
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
val numberStrings = numbers.filter { it % 2 == 0 }.map { "Number $it" }
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents