Bird
Raised Fist0
Kotlinprogramming~10 mins

Sequence operators (map, filter) in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Sequence operators (map, filter)
Start with a Sequence
↓
Apply map operator
↓
Transform each element
↓
Apply filter operator
↓
Keep elements matching condition
↓
Resulting Sequence
↓
Convert to List or use
↓
End
Start with a sequence, transform elements with map, then keep only those matching a condition with filter, producing a new sequence.
Execution Sample
Kotlin
val numbers = sequenceOf(1, 2, 3, 4, 5)
val result = numbers.map { it * 2 }.filter { it > 5 }.toList()
println(result)
This code doubles each number, then keeps only those greater than 5, and prints the final list.
Execution Table
StepElementAfter map (it * 2)Filter condition (it > 5)Kept in result?
1122 > 5 = falseNo
2244 > 5 = falseNo
3366 > 5 = trueYes
4488 > 5 = trueYes
551010 > 5 = trueYes
End---No more elements
💡 All elements processed; sequence ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
numbers[1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5]
mappedempty[2][2,4][2,4,6][2,4,6,8][2,4,6,8,10][2,4,6,8,10]
filteredemptyemptyempty[6][6,8][6,8,10][6,8,10]
Key Moments - 3 Insights
Why does the filter keep only some elements after map?
Because filter checks the mapped values against the condition (it > 5). Only elements with mapped values greater than 5 are kept, as shown in execution_table rows 3-5.
Does map change the original sequence?
No, map creates a new sequence with transformed elements without changing the original, as variable_tracker shows 'numbers' stays the same.
When does the sequence stop processing elements?
After all elements are processed, as indicated by the 'End' row in execution_table and the final states in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the mapped value of element 4?
A8
B6
C4
D10
💡 Hint
Check the 'After map (it * 2)' column for Step 4 in execution_table.
At which step does the filter condition become true for the first time?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the 'Filter condition (it > 5)' column in execution_table to find when it first says true.
If the filter condition changed to it > 8, which elements would be kept?
A[6, 8, 10]
B[8, 10]
C[10]
D[2, 4, 6]
💡 Hint
Refer to the mapped values in variable_tracker and apply the new condition it > 8.
Concept Snapshot
Sequence operators map and filter in Kotlin:
- map transforms each element.
- filter keeps elements matching a condition.
- Both return new sequences.
- Use toList() to get a list.
- Processing is lazy until terminal operation.
Full Transcript
This example shows how Kotlin sequences use map and filter operators. We start with a sequence of numbers 1 to 5. The map operator doubles each number, creating a new sequence of doubled values. Then filter keeps only those doubled values greater than 5. The execution table traces each element through these steps, showing which elements pass the filter. The variable tracker shows how the original sequence stays unchanged, while mapped and filtered sequences build up. Key moments clarify common confusions about sequence immutability and lazy processing. The visual quiz tests understanding of mapped values, filter conditions, and how changing conditions affects results. The concept snapshot summarizes the key points about map and filter on sequences in Kotlin.

Practice

(1/5)
1. What does the map operator do in a Kotlin sequence?
easy
A. It removes items that do not match a condition.
B. It transforms each item in the sequence to a new value.
C. It sorts the items in the sequence.
D. It counts the number of items in the sequence.

Solution

  1. Step 1: Understand the purpose of map

    The map operator applies a function to each item, changing it to a new value.
  2. Step 2: Compare with other operators

    filter removes items, sort orders items, and count counts items, so these are different.
  3. Final Answer:

    It transforms each item in the sequence to a new value. -> Option B
  4. Quick Check:

    map = transform each item [OK]
Hint: Map changes items; filter removes items [OK]
Common Mistakes:
  • Confusing map with filter
  • Thinking map sorts or counts items
  • Assuming map removes items
2. Which of the following is the correct syntax to filter even numbers from a sequence seq in Kotlin?
easy
A. seq.filter { it % 2 == 0 }
B. seq.map { it % 2 == 0 }
C. seq.filter(it % 2 == 0)
D. seq.map { it / 2 == 0 }

Solution

  1. Step 1: Identify correct filter syntax

    In Kotlin, filter takes a lambda with condition inside curly braces: { it % 2 == 0 }.
  2. Step 2: Check other options

    map changes values, not filters; seq.filter(it % 2 == 0) misses braces; seq.map { it / 2 == 0 } uses wrong condition.
  3. Final Answer:

    seq.filter { it % 2 == 0 } -> Option A
  4. Quick Check:

    filter uses braces and condition [OK]
Hint: filter uses braces with condition inside [OK]
Common Mistakes:
  • Using map instead of filter
  • Missing curly braces in lambda
  • Wrong condition inside lambda
3. What is the output of this Kotlin code?
val numbers = sequenceOf(1, 2, 3, 4)
val result = numbers.filter { it % 2 == 0 }.map { it * 10 }.toList()
println(result)
medium
A. [10, 20]
B. [2, 4]
C. [20, 40]
D. [1, 3]

Solution

  1. Step 1: Apply filter to keep even numbers

    Filter keeps numbers where it % 2 == 0, so 2 and 4 remain.
  2. Step 2: Apply map to multiply each by 10

    Mapping multiplies each by 10: 2 * 10 = 20, 4 * 10 = 40.
  3. Final Answer:

    [20, 40] -> Option C
  4. Quick Check:

    filter evens then map *10 = [20, 40] [OK]
Hint: Filter first, then map transforms filtered items [OK]
Common Mistakes:
  • Mapping before filtering
  • Confusing filter condition
  • Forgetting to convert sequence to list
4. Find the error in this Kotlin code snippet:
val seq = sequenceOf(1, 2, 3, 4)
val filtered = seq.filter(it % 2 == 0)
println(filtered.toList())
medium
A. Missing curly braces around lambda in filter
B. Using map instead of filter
C. Sequence cannot be converted to list
D. Filter condition should be it % 2 != 0

Solution

  1. Step 1: Check filter lambda syntax

    Filter requires a lambda with curly braces: { it % 2 == 0 }. The code misses braces.
  2. Step 2: Validate other options

    Map is not used here; sequence can convert to list; condition can be any boolean expression.
  3. Final Answer:

    Missing curly braces around lambda in filter -> Option A
  4. Quick Check:

    filter needs braces for lambda [OK]
Hint: Always use braces {} for lambdas in filter [OK]
Common Mistakes:
  • Omitting braces in lambda
  • Confusing filter and map
  • Wrong filter condition assumed as error
5. Given a sequence of strings seq = sequenceOf("apple", "banana", "cherry", "date"), which Kotlin code correctly filters strings containing letter 'a' and converts them to uppercase?
hard
A. seq.filter { it.contains('a') }.map { it.lowercase() }.toList()
B. seq.map { it.uppercase() }.filter { it.contains('a') }.toList()
C. seq.map { it.lowercase() }.filter { it.contains('a') }.toList()
D. seq.filter { it.contains('a') }.map { it.uppercase() }.toList()

Solution

  1. Step 1: Filter strings containing 'a'

    Use filter { it.contains('a') } to keep only strings with 'a'. This keeps "apple", "banana", "date".
  2. Step 2: Convert filtered strings to uppercase

    Use map { it.uppercase() } to change these strings to uppercase.
  3. Final Answer:

    seq.filter { it.contains('a') }.map { it.uppercase() }.toList() -> Option D
  4. Quick Check:

    Filter then map uppercase = seq.filter { it.contains('a') }.map { it.uppercase() }.toList() [OK]
Hint: Filter first, then map to uppercase [OK]
Common Mistakes:
  • Mapping before filtering changes all strings
  • Using lowercase instead of uppercase
  • Filtering after mapping changes string case before filter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard