Ranges as Collections
Map and filter over range sequences.
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)All lessons in this course
- Closed and Half-Open Ranges
- One-Sided Ranges
- Counting with stride(from:to:by:)
- Ranges as Collections