Introduction
Kotlin's standard library provides a large set of extension functions on collections that are inspired by functional programming languages. Operations like map, filter, fold, reduce, sortedBy, and groupBy let you transform and process data declaratively, chaining multiple steps together instead of writing manual loops. These operations are eager by default, meaning each call produces a new intermediate collection.
Cricket analogy: Chaining map, filter, fold, sortedBy, groupBy on a ball-by-ball dataset is like a commentator declaratively deriving 'top six-hitters this over' from raw deliveries, but each step (like grouping by bowler) produces a fresh intermediate scorecard.
Syntax
val doubled = list.map { it * 2 }
val evens = list.filter { it % 2 == 0 }
val sum = list.fold(0) { acc, x -> acc + x }
val product = list.reduce { acc, x -> acc * x }
val sorted = list.sortedBy { it }
val grouped = list.groupBy { it % 2 == 0 }Explanation
map { } applies a transformation to each element and returns a new list of the results. filter { } keeps only the elements for which the given predicate lambda returns true. fold(initial) { acc, x -> ... } combines all elements into a single accumulated value, starting from an explicit seed value; this makes it safe on empty collections since the seed is always returned when there are no elements. reduce { acc, x -> ... } behaves like fold but uses the first element of the collection as the initial accumulator, so it throws an exception if called on an empty collection. These operations can be chained together, and because each eager call materializes a new collection, chaining many operations on very large collections can create several intermediate lists.
Cricket analogy: map{} converting each delivery to 'runs scored' is like a scorer transforming every ball into a run tally; filter{} keeps only boundary balls; fold(0){acc,x->...} sums total runs safely even on a rain-abandoned (empty) innings, while reduce would crash with no first ball to seed from.
Example
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers
.filter { it % 2 != 0 }
.map { it * it }
.fold(0) { acc, x -> acc + x }
println(result)
println(numbers.reduce { acc, x -> acc + x })
}35
15- map { } transforms each element into a new value, producing a list of the same size.
- filter { } keeps only elements matching a predicate, producing a list of equal or smaller size.
- fold(initial) { } reduces a collection to one value starting from a given seed, safe on empty collections.
- reduce { } is like fold but uses the first element as the seed, and throws on empty collections.
- These operations are eager and chainable, but each step creates a new intermediate collection.
Practice what you learned
1. What does list.map { it * 2 } return?
2. What is the key difference between fold and reduce?
3. What happens if reduce is called on an empty list?
4. Which function keeps only elements matching a given condition?
5. Why is fold considered safer than reduce for empty collections?
Was this page helpful?
You May Also Like
List, Set, and Map in Kotlin
A detailed look at Kotlin's three core collection types — List, Set, and Map — and how each stores and accesses data.
Sequences in Kotlin
How Kotlin Sequences provide lazy, element-by-element evaluation of collection operations for better performance on large chains.
Lambda Expressions in Kotlin
Understand how to write and use lambda expressions in Kotlin, including trailing lambda syntax and the implicit it parameter.
Higher-Order Functions in Kotlin
Learn how Kotlin functions can accept or return other functions, enabling flexible, reusable, functional-style code.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics