Kotlin combines object-oriented programming with a functional style: treat functions as values, favor immutable data, and limit side effects. This approach makes behavior easier to compose, reuse, test, and reason about. section 1.2.3
Passing behavior as a value
A Java-style listener requires an anonymous object and an overridden method. Kotlin can pass the behavior directly: button.setOnClickListener { println("I was clicked!") } The lambda replaces the verbose listener implementation while preserving the same callback behavior. section 5.1.1
Functional code is useful because it:
- Reduces mutation, loops, and branching by composing functions and passing differing behavior as arguments. section 1.2.3
- Avoids duplicated logic by extracting common operations and supplying the variable parts as functions or lambdas. section 1.2.3
- Supports safer concurrent code because immutable data and pure functions prevent unsafe shared modifications. section 1.2.3
- Simplifies testing because functions without side effects can be tested in isolation with less environmental setup. section 1.2.3
messages
.filter { it.body.isNotBlank() && !it.isRead }
.map(Message::sender)
.distinct()
.sortedBy(Sender::name)
A collection pipeline demonstrates functional composition: filter unwanted messages, map them to senders, remove duplicates, and sort the result. section 1.2.3
Kotlin’s collection APIs turn common transformations into a shared vocabulary: functions such as filter and map accept lambdas instead of requiring manual traversal and aggregation. Sequences extend this style with lazy execution for multiple operations, while collections remain eager. section 6.1 section 6.0


















