Swift Academy · Lesson

Map, Filter and Reduce on Arrays

Transforming arrays with functional higher-order functions.

Lesson 3 of 412 steps

Map, Filter and Reduce on Arrays is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

Swift's higher-order functions — `map`, `filter`, and `reduce` — transform collections without mutation. They make your intentions explicit and eliminate error-prone loops.

map — Transform Every Element

```swift let nums = [1, 2, 3, 4] let doubled = nums.map { $0 * 2 } // [2,4,6,8] let strings = nums.map { "Item \($0)" } // ["Item 1","Item 2",...] ``` `map` applies a closure to each element and returns a new array of the results.

filter — Keep Matching Elements

```swift let scores = [45, 80, 92, 37, 68] let passing = scores.filter { $0 >= 60 } // [80,92,68] let even = (1...10).filter { $0 % 2 == 0 } // [2,4,6,8,10] ``` `filter` returns a new array containing only elements for which the closure returns `true`.

reduce — Combine Into One Value

```swift let nums = [1, 2, 3, 4, 5] let sum = nums.reduce(0) { $0 + $1 } // 15 let product = nums.reduce(1, *) // 120 let joined = ["a","b","c"].reduce("") { $0 + $1 } // "abc" ```

Chaining map, filter, reduce

```swift let result = (1...10) .filter { $0 % 2 == 0 } // [2,4,6,8,10] .map { $0 * $0 } // [4,16,36,64,100] .reduce(0, +) // 220 ``` Chaining reads left-to-right, expressing the data pipeline clearly.

compactMap — Map and Unwrap

```swift let strings = ["1","two","3","four","5"] let nums = strings.compactMap { Int($0) } // [1,3,5] ``` `compactMap` transforms and removes nil results — perfect for fallible conversions.

flatMap — Flatten Nested Arrays

```swift let nested = [[1,2,3],[4,5],[6]] let flat = nested.flatMap { $0 } // [1,2,3,4,5,6] let words = ["hello world","foo bar"] let all = words.flatMap { $0.split(separator: " ") } // ["hello","world","foo","bar"] ```

forEach — Side Effects Only

```swift [1, 2, 3].forEach { print($0) } ``` `forEach` is like a `for-in` loop but cannot use `break` or `continue`. Use it only when you need side effects and don't need to escape early.

reduce(into:) for Dictionaries

```swift let words = ["apple","banana","apricot","blueberry"] let grouped = words.reduce(into: [Character:[String]]()) { dict, word in let key = word.first! dict[key, default: []].append(word) } // ["a":["apple","apricot"],"b":["banana","blueberry"]] ```

Lazy Chains for Performance

```swift let lazyResult = (1...1_000_000) .lazy .filter { $0 % 7 == 0 } .map { $0 * 2 } .prefix(5) // No intermediate arrays created! ``` Add `.lazy` at the start of a chain to evaluate on demand.

Quick Check

Which function transforms each element and removes nil results?

Recap

Key takeaways: • `map` — transform every element into a new array • `filter` — keep elements matching a predicate • `reduce` — fold into a single accumulated value • `compactMap` — transform + discard nils • `flatMap` — transform + flatten nested sequences • Chain with `.lazy` to avoid intermediate allocations Next: 2D arrays and nested collections.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Map, Filter and Reduce on Arrays” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Map, Filter and Reduce on Arrays”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Map, Filter and Reduce on Arrays”?

Transforming arrays with functional higher-order functions. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Map, Filter and Reduce on Arrays” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating and Mutating Arrays
  2. Sorting, Searching and Slicing
  3. Map, Filter and Reduce on Arrays
  4. 2D Arrays and Nested Collections
← Back to Swift Academy