Ranges as Collections
Map and filter over range sequences.
Ranges as Collections is a free Swift Academy lesson on CoddyKit. This is lesson 4 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.
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 })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 “Ranges as Collections” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Ranges as Collections”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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
- Closed and Half-Open Ranges
- One-Sided Ranges
- Counting with stride(from:to:by:)
- Ranges as Collections