Functional constructs for iteration

suggest change

The 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 of the many advantages of this style is it allows to chain operations in a similar fashion. Only a minor modification would be required if say, the list above were needed to be filtered for even numbers. The filter function can be used.

val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
val numberStrings = numbers.filter { it % 2 == 0 }.map { "Number $it" }

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents