ProgramGuru

Map, filter, fold and sequences

Map, filter, fold and sequences

A teacher wants corrected scores, passing scores and a total. Map transforms every element into an output element. Filter keeps elements satisfying a condition. Reduce combines elements into a single accumulated result. These names describe distinct operations rather than interchangeable shortcuts.

What you’ll learn

  • Trace independent collection transformations.
  • Compare eager results and lazy sequences.

Map, filter, fold and sequences in the example

  1. Original scores

    A teacher wants corrected scores, passing scores and a total.

    Original scores
  2. Separate transformations

    Map transforms every element, filter keeps matching elements and fold combines values from a supplied initial accumulator.

    Separate transformations
  3. New values and total

    The corrected list is [65, 80, 95], the passing original scores are [75, 90] and the folded total is 225.

    New values and total
The corrected list is [65, 80, 95], the passing original scores are [75, 90] and the folded total is 225. Each operation uses scores independently; creating one transformed list does not overwrite the original.

Understand map, filter, fold and sequences

Map transforms every element, filter keeps matching elements and fold combines values from a supplied initial accumulator. The lambda parameter it names a single argument when not explicitly named. AsSequence can defer intermediate operations until a terminal operation requests results.

Connect this to lambdas and higher-order functions and read-only and mutable lists.

Follow map, filter, fold and sequences

Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.

Kotlin/JVM · complete program

fun main() {
  val scores = listOf(60, 75, 90)
  println(scores.map { it + 5 })
  println(scores.filter { it >= 75 })
  println(scores.fold(0) { total, score -> total + score })
}

Expected output

[65, 80, 95]
[75, 90]
225

Follow the execution

The corrected list is [65, 80, 95], the passing original scores are [75, 90] and the folded total is 225. Each operation uses scores independently; creating one transformed list does not overwrite the original.

Try this in the Kotlin compiler by pasting the complete program.

Practice: Map, filter, fold and sequences

Your task: Why does the passing list omit 60 despite creating corrected scores?

Kotlin/JVM · complete program

fun main() {
  val scores = listOf(60, 75, 90)
  println(scores.map { it + 5 })
  println(scores.filter { it >= 75 })
  println(scores.fold(0) { total, score -> total + score })
}

Worked answer

Filter reads the original scores, not the separate map result. Its condition applies directly to each original value.

Map, filter, fold and sequences: the important boundary

Reduce has no supplied initial accumulator and needs handling for an empty collection; fold can return its initial value for empty input. Lazy sequences can avoid intermediate lists, but overhead and evaluation order mean they are not automatically faster for every task.

Check your understanding

Why does the passing list omit 60 despite creating corrected scores?
Choose one answer. You can try again.

Answer and explanation

Filter reads the original scores, not the separate map result.

Filter reads the original scores, not the separate map result. Its condition applies directly to each original value.

Map, filter, fold and sequences: what to keep in mind

Filter reads the original scores, not the separate map result. Its condition applies directly to each original value. Reduce has no supplied initial accumulator and needs handling for an empty collection; fold can return its initial value for empty input.

Further reading: official Kotlin documentation for this topic.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Functions and return values →