liveBook · Manning

Chapter 17: Flow operators

What you will learn in chapter 17 of Kotlin in Action, 2E

This section introduces flow operators and distinguishes operators that build a modified flow from those that collect and execute it.

After this chapter you will be able to

What it builds on

Section by section

Introduction

17.1 Manipulating flows with flow operators

This section introduces flow operators and distinguishes operators that build a modified flow from those that collect and execute it.

Key terms: intermediate flow operators, terminal flow operators, modified flow, collecting the flow

17.2 Intermediate operators are applied to an upstream flow and return a downstream flow

This section defines upstream and downstream flows in an operator chain and explains that intermediate flow operators defer their behavior until the flow is collected.

Key terms: intermediate operator, upstream flow, downstream flow, cold, hot flow, terminal operator, collect, map, filter, onEach

17.2.1 Emitting arbitrary values for each upstream element: The transform function

This subsection shows how transform can emit multiple downstream values for each upstream flow element and introduces flowOf as a shorthand for creating a flow from values.

Key terms: map, transform, upstream flow, downstream flow, flowOf, flow operators

17.2.2 The take operator family can cancel a flow

This subsection explains how take-family operators stop a flow by cancelling its upstream flow when their condition is no longer met.

Key terms: take, takeWhile, upstream flow, cancelled

17.2.3 Hooking into flow phases with onStart, onEach, onCompletion, and onEmpty

This section explains how intermediate flow operators run logic at different lifecycle phases and why their position in a chain affects which emissions they receive.

Key terms: onStart, onEach, onCompletion, onEmpty, intermediate operators, upstream, downstream

17.2.4 Buffering elements for downstream operators and collectors: The buffer operator

This section explains how buffer lets a cold flow produce elements while downstream processing continues, and how to choose what happens when the buffer fills.

Key terms: cold flow, buffer operator, producer, collector, downstream flow, onBufferOverflow, SUSPEND, DROP_OLDEST, DROP_LATEST

17.2.5 Throwing away intermediate values: The conflate operator

This section explains how conflate lets a slow collector skip intermediate flow values while the producer continues emitting.

Key terms: conflate, upstream flow, collector, intermediate elements

17.2.6 Filtering out values on a timeout: The debounce operator

Explains how debounce delays flow values until input pauses, avoiding unnecessary search requests while a user types.

Key terms: debounce operator, timeout, upstream, downstream flow, searchQuery flow

17.2.7 Switching the coroutine context on which a flow is executed: The flowOn operator

This subsection explains how flowOn changes the coroutine context for part of a flow pipeline without changing the downstream context.

Key terms: coroutine context, collect, flowOn, dispatcher, upstream flow, downstream flow, context preserving

17.3 Creating custom intermediate operators

Shows how to build a custom intermediate flow operator by collecting an upstream flow inside a new, cold downstream flow.

Key terms: intermediate operator, upstream flow, downstream flow, collect, flow builder, cold

17.4 Terminal operators execute the upstream flow and may compute a value

This section explains how terminal operators execute a flow, using collect and first as examples.

Key terms: intermediate operators, terminal operators, upstream flow, suspending functions, collect, onEach, first, firstOrNull

17.4.1 Frameworks provide custom operators

This section shows how Compose converts a flow into UI state that can trigger recomposition.

Key terms: Jetpack Compose, Compose Multiplatform, collectAsState, State, recomposition, initial value

17.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 16: Flows Chapter 18: Error handling and testing →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.