Collection Operations (map, filter, fold) (Kotlin)
Kotlin provides functional operations on collections — map, filter, fold, groupBy, and more — that transform data declaratively with lambdas instead of manual loops.
Kotlin
These are higher-order extension functions on collections. map transforms each element, filter keeps elements matching a predicate, fold/reduce accumulate to a single value, and others like groupBy, partition, associate, sortedBy, and sumOf cover common data-shaping tasks.
They chain fluently: list.filter { it > 0 }.map { it * 2 }.sum(). By default each step builds a new eager list; for large pipelines, .asSequence() makes them lazy so elements flow through the whole chain one at a time, avoiding intermediate lists.
Using these operations instead of for loops makes intent obvious and bugs rarer. They mirror Java Streams and Rust iterators but read more concisely thanks to trailing-lambda syntax and the implicit it.
Syntax
list.filter { it > 0 }.map { it * it }.sum()Examples
Kotlin
val nums = listOf(1, 2, 3, 4, 5)
val result = nums.filter { it % 2 == 1 }.map { it * it }
println(result)
val total = nums.fold(0) { acc, n -> acc + n }
println(total)Output:
[1, 9, 25]
15