filter and Predicates
Select elements that satisfy a condition.
filter and Predicates is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is filter?
filter keeps only the elements for which a closure (a predicate) returns true.
- The closure returns a
Bool - The element type stays the same
- The result is a new array, possibly smaller
A Simple filter
Keep only the even numbers:
let numbers = [1, 2, 3, 4, 5, 6]
let evens = numbers.filter { $0 % 2 == 0 }
print(evens) // [2, 4, 6]Filtering Strings
Keep words longer than three characters:
let words = ["hi", "swift", "go", "filter"]
let long = words.filter { $0.count > 3 }
print(long) // ["swift", "filter"]What Is a Predicate?
A predicate is simply a function that takes a value and returns a Bool. filter calls your predicate once per element and keeps the value when the predicate is true.
Naming a Predicate
Predicates can be stored and reused for readability:
let isPositive: (Int) -> Bool = { $0 > 0 }
let data = [-2, 5, -1, 8]
print(data.filter(isPositive)) // [5, 8]Combining Conditions
Use boolean operators inside the predicate:
let nums = [1, 2, 3, 4, 5, 6, 7, 8]
let picked = nums.filter { $0 % 2 == 0 && $0 > 4 }
print(picked) // [6, 8]Filtering Structs
Filter a collection of model objects on a property:
struct Product { let name: String; let price: Int }
let items = [Product(name: "Pen", price: 2), Product(name: "Bag", price: 40)]
let cheap = items.filter { $0.price < 10 }
print(cheap.map { $0.name }) // ["Pen"]filter vs map
They answer different questions: filter chooses which elements stay; map changes each element. Result types differ:
let n = [1, 2, 3, 4]
print(n.filter { $0 > 2 }) // [3, 4] (subset, same type)
print(n.map { $0 > 2 }) // [false, false, true, true]Chaining filter and map
Filter first to shrink the work, then transform:
let nums = [1, 2, 3, 4, 5, 6]
let result = nums
.filter { $0 % 2 == 0 }
.map { $0 * $0 }
print(result) // [4, 16, 36]Counting Without Keeping
If you only need a count, filter(...).count works, but count(where:) avoids building an array:
let votes = [true, false, true, true]
let yes = votes.filter { $0 }.count
print(yes) // 3Filtering a Dictionary
filter on a dictionary yields a dictionary of matching key/value pairs:
let ages = ["Ann": 30, "Bo": 17, "Cy": 22]
let adults = ages.filter { $0.value >= 18 }
print(adults.keys.sorted()) // ["Ann", "Cy"]Quick Check
What must a filter predicate return?
Recap
You learned filtering:
filterkeeps elements where the predicate istrue- A predicate is any value-to-
Boolfunction - Element type is preserved; the array may shrink
- Combine conditions with
&&/||and chain withmap
Next: reduce.
Frequently asked questions
Is the “filter and Predicates” lesson free?
Yes — the full text of “filter and Predicates” is free to read here on the web, and the Swift Academy course includes 4 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 “filter and Predicates”?
Select elements that satisfy a condition. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “filter and Predicates” 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
- map and compactMap
- filter and Predicates
- reduce and reduce(into:)
- flatMap and Chaining