Generic algorithms on collections
Use generic algorithms like map , filter , and reduce , and write your own functions over Sequence with simple constraints.
Why generic algorithms?
Swift provides powerful, generic algorithms on Sequence/Collection: map, filter, and reduce. You can also write your own generic helpers.
map
map applies a transform to every element, returning a new array.
let nums = [1, 2, 3]
let doubled = nums.map { $0 * 2 } // [2, 4, 6]
print(doubled)
let words = ["swift", "kit"]
let caps = words.map { $0.uppercased() } // ["SWIFT","KIT"]
print(caps)All lessons in this course
- Generic functions & types
- Constraints (where), type inference
- Generic algorithms on collections