0Pricing
Swift Academy · Lesson

Ranges as Collections

Map and filter over range sequences.

Ranges as Collections is a free Swift Academy lesson on CoddyKit — lesson 4 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.

Ranges Are Sequences

A range is more than a span of numbers; it is a sequence you can transform with collection methods like map and filter.

let squares = (1...5).map { $0 * $0 }
print(squares)

Mapping a Range

(1...n).map { ... } turns each number into a new value, producing an array. Note the parentheses around the range.

let doubled = (1...4).map { $0 * 2 }
print(doubled)

Why the Parentheses

You must wrap the range in parentheses before calling a method, otherwise Swift misreads the syntax. (1...5).map is correct.

let labels = (1...3).map { "Item \($0)" }
print(labels)

Filtering a Range

filter keeps only the values that satisfy a condition, returning an array of matches.

let evens = (1...10).filter { $0 % 2 == 0 }
print(evens)

Reducing a Range

reduce combines all values into one. Here we sum the numbers 1 through 100.

let total = (1...100).reduce(0, +)
print(total)

Converting to an Array

Wrapping a range in Array(...) materializes every value as a stored array.

let nums = Array(1...5)
print(nums)
print(nums.count)

forEach on a Range

You can call forEach to run a closure for each value, similar to a for loop.

(1...3).forEach { n in
    print("Tick \(n)")
}

Chaining Operations

Because each operation returns a collection, you can chain filter then map.

let result = (1...10).filter { $0 % 2 == 0 }.map { $0 * $0 }
print(result)

Building Lookup Data

Mapping a range is a quick way to generate test data or lookup tables.

let table = (1...5).map { (n: $0, square: $0 * $0) }
for row in table {
    print("\(row.n) -> \(row.square)")
}

contains and Aggregates

Ranges support aggregate queries like contains, min, and max as well.

let r = 5...15
print(r.contains(10))
print(r.min() ?? 0)
print(r.max() ?? 0)

Counting Matches

Combine filter with count to count how many values meet a condition.

let multiplesOf3 = (1...30).filter { $0 % 3 == 0 }.count
print(multiplesOf3)

Quick Check

Test ranges as collections.

Recap: Ranges as Collections

Ranges are sequences, so you can call map, filter, reduce, and forEach on them (remember the parentheses). Use Array(range) to materialize the values.

print((1...5).map { $0 * $0 })

Frequently asked questions

Is the “Ranges as Collections” lesson free?

Yes — the full text of “Ranges as Collections” 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 “Ranges as Collections”?

Map and filter over range sequences. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Ranges as Collections” 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. Closed and Half-Open Ranges
  2. One-Sided Ranges
  3. Counting with stride(from:to:by:)
  4. Ranges as Collections
← Back to Swift Academy