Swift Academy · Lesson

Generic algorithms on collections

Use generic algorithms like map , filter , and reduce , and write your own functions over Sequence with simple constraints.

Lesson 3 of 38 steps

Generic algorithms on collections is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 3. 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 3 lessons in total.

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 }) // false

Tips & 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.

Free to start

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 “Generic algorithms on collections” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Generic algorithms on 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 3 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

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

  1. Generic functions & types
  2. Constraints (where), type inference
  3. Generic algorithms on collections
← Back to Swift Academy