liveBook · Manning

Functional, stateless philosophy

As explained in Kotlin in Action, 2E

Functional programming treats computation primarily as the evaluation and composition of functions, often emphasizing immutable data and limited side effects.

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

How Kotlin in Action, 2E covers it

How deep it goes
Defines first-class functions, immutability, and pure functions, then demonstrates lambdas and collection operations without descending into implementation internals.
Examples
Uses a button listener lambda and a message pipeline applying filter, map, distinct, and sortedBy.
What it assumes
Assumes basic Kotlin and object-oriented familiarity while teaching functional principles through comparisons with Java listener syntax.
What you can do after
Apply it via Kotlin lambdas and standard collection functions to compose behavior and express data transformations.
How it connects
Connects first-class functions with lambda expressions, immutability, pure functions, collections, sequences, concurrency, and testing.

Also explained in

Compare how 19 books explain functional, stateless philosophy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.