ProgramGuru

Transform collections with map, filter and reduce

Transform collections with map, filter and reduce

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 the separate results of map, filter and reduce.
  • Preserve the original collection while naming transformed stages.

Transform collections with map, filter and reduce in the example

  1. Original scores

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

    Original scores
  2. Separate transformations

    The expressions in braces are closures, blocks of executable code supplied as values.

    Separate transformations
  3. New values and total

    Map yields [65, 80, 95], keeping one output per input.

    New values and total
Map yields [65, 80, 95], keeping one output per input. Filter uses the original scores, not corrected, and keeps 75 and 90. Reduce accumulates 0 + 60 + 75 + 90, producing 225. The original array remains unchanged.

How transform collections with map, filter and reduce works

The expressions in braces are closures, blocks of executable code supplied as values. $0 names the first closure argument. In reduce, $0 is the accumulator and $1 is the next element. The starting value gives the initial accumulator state.

Work through transform collections with map, filter and reduce

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

let scores = [60, 75, 90]
let corrected = scores.map { $0 + 5 }
let passing = scores.filter { $0 >= 75 }
let total = scores.reduce(0) { $0 + $1 }
print(corrected)
print(passing)
print(total)

Expected output

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

Follow the execution

Map yields [65, 80, 95], keeping one output per input. Filter uses the original scores, not corrected, and keeps 75 and 90. Reduce accumulates 0 + 60 + 75 + 90, producing 225. The original array remains unchanged.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Transform collections with map, filter and reduce

Your task: Why is 60 absent from passing even though you created corrected scores?

Swift · complete program

let scores = [60, 75, 90]
let corrected = scores.map { $0 + 5 }
let passing = scores.filter { $0 >= 75 }
let total = scores.reduce(0) { $0 + $1 }
print(corrected)
print(passing)
print(total)

Worked answer

Passing is filtered from scores. Assigning corrected does not replace the original list or redirect another expression automatically.

Trace each transformation separately

Long chains can obscure intermediate meaning and create intermediate collections. Name useful stages when clarity matters. compactMap combines transformation with removal of nil results, and differs from filter because the surviving values can have a new type.

Check your decision

Why is 60 absent from passing even though you created corrected scores?
Choose one answer. You can try again.

Answer and explanation

Passing is filtered from scores.

Passing is filtered from scores. Assigning corrected does not replace the original list or redirect another expression automatically.

Trace each transformation separately

Passing is filtered from scores. Assigning corrected does not replace the original list or redirect another expression automatically. Long chains can obscure intermediate meaning and create intermediate collections.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Functions: name reusable work →