Kotlin typing practice: follow a collection chain

When a Kotlin expression continues across several lines, use each collection stage as a stopping point. Follow a cart label from data class to filtered items to formatted strings, then assemble the chain one stage at a time.

Language practice guide
In this guide, you’ll practice

Predict the filtered items → type the next lambda → check the output

01 Read a Kotlin chain with filter and map

A leading dot starts the next operation on the same collection. Read .filter { … } as a predicate, then .map { … } as a transformation. The braces close each lambda; ${…} inside the string has a separate job.

CartLabels.ktChain excerpt
.filter { it.qty > 0 }
.map { "${it.sku}: ${it.qty}" }

02 Trace and change the cart labels

CartLabels.ktCollection example
data class Line(
    val sku: String,
    val qty: Int
)

val lines = listOf(
    Line("BK", 2), Line("INK", 0)
)
val labels = lines
    .filter { it.qty > 0 }
    .map { "${it.sku}: ${it.qty}" }
lines · List<Line>
Two entries: BK with quantity 2, INK with quantity 0.
filter · List<Line>
The predicate returns a Boolean. The resulting list keeps BK; the entries are still Line values.
map · List<String>
The lambda builds a string from each remaining Line. The output contains one string: "BK: 2".

Change the filter to keep quantities of 2 or more. Before running it, predict what happens when INK has quantity 1, then 2. Do not change the map lambda: it still formats the items that made it through.

Reveal the two resulting lists

Expanded stage predictions

INK = 1  →  ["BK: 2"]
INK = 2  →  ["BK: 2", "INK: 2"]

With qty >= 2, BK stays in both cases. INK enters the list only at 2. The filter changes membership; map still builds the label. The baseline and both changed inputs passed on Kotlin/JVM.

03 Practice with Kotlin code

Start in your editor, then carry one pattern into the timed test. The article example and the test passage are separate.

  1. 01
    Build the input list

    Type the data class and sample list first. Read each constructor call as one unit, then check the two quantities before continuing.

  2. 02
    Add one lambda at a time

    Type .filter and check what remains before adding .map. Read each ${…} as a complete expression inside the string.

  3. 03
    Try the same transition in a Kotlin test

    Watch the leading dot and the closing brace of each lambda. If you lose your place, practice only that stage boundary in your editor before another run.

Try the Kotlin typing test

Look for the same typing pattern in the displayed passage.

Start Kotlin typing test

04 Review the copy and the result

Check membership and text separately: filter decides which items stay, and map decides what each label says. If the output is correct but you mistyped a brace or template, record it as a copying error.

In the timed test, record the language, duration, CPM, accuracy and one transition to revisit. Compare runs of similar duration and mode; different snippets can change the result.

Understand CPM and WPM for code →

05 Keep learning