0Pricing
Swift Academy · Lesson

Map, Filter and Reduce on Arrays

Transforming arrays with functional higher-order functions.

Map, Filter and Reduce on Arrays is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “Map, Filter and Reduce on Arrays” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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