Generic algorithms on collections
Use generic algorithms like map , filter , and reduce , and write your own functions over Sequence with simple constraints.
Generic algorithms on collections is a free Swift Academy lesson on CoddyKit — lesson 3 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)filter
filter keeps elements that satisfy a predicate.
let scores = [45, 72, 88, 53]
let passing = scores.filter { $0 >= 60 } // [72, 88]
print(passing)
let names = ["Ana","Li","Max"]
let short = names.filter { $0.count <= 2 } // ["Li"]
print(short)reduce
reduce folds a sequence into a single result, using a starting value and a combine closure.
let sum = [1,2,3,4].reduce(0) { acc, x in acc + x } // 10
print(sum)
let joined = ["a","b","c"].reduce("") { acc, s in acc + s } // "abc"
print(joined)Custom generic (Sequence)
Create your own generic algorithm by constraining the input to Sequence and using its Element.
// Works for any Sequence whose Element meets the predicate
func allMatch<S: Sequence>(_ seq: S, _ predicate: (S.Element) -> Bool) -> Bool {
for x in seq { if !predicate(x) { return false } }
return true
}
print(allMatch([2,4,6]) { $0 % 2 == 0 }) // true
print(allMatch(["ab","c"]) { $0.count >= 2 }) // falseTips & patterns
Tips:
- Prefer clear closures: name parameters if readability suffers.
- Chain map → filter → reduce for pipelines.
- Keep heavy work outside hot loops for performance.
Reduce meaning
Quick check: Which operation combines a sequence into one value with a starting value?
Recap
Recap: Use map to transform, filter to select, and reduce to combine. You can also write your own generic helpers over Sequence.
Frequently asked questions
Is the “Generic algorithms on collections” lesson free?
Yes — the full text of “Generic algorithms on collections” is free to read here on the web, and the Swift Academy course includes 3 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 “Generic algorithms on collections”?
Use generic algorithms like map , filter , and reduce , and write your own functions over Sequence with simple constraints. 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 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Generic algorithms on 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
- Generic functions & types
- Constraints (where), type inference
- Generic algorithms on collections