Map/Filter/Reduce, lazy sequences
Use map / filter / reduce to transform, select, and combine; then add .lazy to defer work and avoid intermediate arrays.
Core idea
Core algorithms:
- map: transform each element
- filter: keep elements matching a predicate
- reduce: combine into one value
- .lazy: defer work to avoid temporary arrays
map
map transforms each element and returns a new array.
let nums = [1, 2, 3]
let doubled = nums.map { $0 * 2 } // [2, 4, 6]
print(doubled)
let words = ["swift", "kit"]
let upper = words.map { $0.uppercased() } // ["SWIFT","KIT"]
print(upper)All lessons in this course
- Sequence/Collection hierarchies
- Map/Filter/Reduce, lazy sequences
- Custom iterators & performance tips