map, filter & reduce

Higher-order methods transform collections without writing loops. You'll learn map , filter , reduce , compactMap , and sorted — the everyday tools of expressive, functional Swift.

Learn map, filter & reduce in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ map — Transform Every Element

map runs your closure on every element and collects the results into a new array of the same length . It never changes the original.

2️⃣ filter — Keep What Matches

filter keeps only the elements for which your closure returns true , producing a (usually shorter) new array.

3️⃣ reduce — Fold Into One Value

reduce combines a collection into a single value. The first argument is the starting value ; the closure merges the running result with each next element.

4️⃣ compactMap & sorted

compactMap transforms then drops any nil results — ideal for parsing. sorted() returns a new ordered array (ascending by default), and sorted(by:) lets you choose the order.

Your turn. Fill in the blanks to filter and then reduce.

📋 Quick Reference

No blanks this time — just a brief and an outline. Build it, run it, and check your output against the example in the comments.

Practice quiz

What does map(_:) do?

  • Removes elements that fail a test
  • Combines all elements into one value
  • Transforms each element, returning a new array of the same length
  • Sorts the array

Answer: Transforms each element, returning a new array of the same length. map transforms every element with a closure and returns a new array of the same length.

What does filter(_:) return?

  • A new array containing only elements that pass the test
  • A single value
  • The first matching element
  • A Boolean

Answer: A new array containing only elements that pass the test. filter keeps only the elements for which the closure returns true, in a new array.

What does reduce(_:_:) produce?

  • A new array
  • A sorted array
  • An optional
  • A single combined value from all elements

Answer: A single combined value from all elements. reduce combines all elements into a single accumulated value, starting from an initial value.

What is the first argument to reduce?

  • The closure
  • The initial (starting) value
  • The array length
  • A Bool

Answer: The initial (starting) value. reduce takes an initial value first, then a combining closure: reduce(0) { $0 + $1 }.

What does compactMap(_:) do that map does not?

  • It removes nil results, unwrapping the rest
  • It sorts the result
  • It reverses the array
  • It throws on nil

Answer: It removes nil results, unwrapping the rest. compactMap transforms then drops any nil results, returning an array of unwrapped non-optional values.

What does [3, 1, 2].sorted() return?

sorted() returns a NEW array in ascending order: [1, 2, 3]. The original is unchanged.

How do you sort descending with sorted(by:)?

  • sorted(by: { $0 < $1 })
  • sorted(by: { $0 > $1 })
  • sorted(descending: true)
  • sorted().reverse()

Answer: sorted(by: { $0 > $1 }). Passing { $0 > $1 } sorts in descending order.

What is [1, 2, 3].map { $0 * 2 } ?

map doubles each element: [2, 4, 6].

Do map, filter, and reduce mutate the original collection?

  • Yes, all of them
  • No, they each return a new value and leave the original unchanged
  • Only filter mutates
  • Only reduce mutates

Answer: No, they each return a new value and leave the original unchanged. These higher-order methods are non-mutating; they return new results and leave the source intact.

What is ['1', 'x', '3'].compactMap { Int($0) } ?

Int('x') is nil, which compactMap drops, leaving [1, 3].