A predicate is a function that evaluates an input and returns Boolean: true when the input satisfies a condition and false otherwise. Kotlin uses predicates extensively with collection operations such as filter, all, any, none, count, and find. section 6.1.3 section 6.3
Defining and applying a predicate
val canBeInClub27 = { p: Person -> p.age <= 27 } val people = listOf(Person("Alice", 27), Person("Bob", 31)) println(people.any(canBeInClub27)) // true println(people.count(canBeInClub27)) // 1 println(people.find(canBeInClub27)) // Person(name=Alice, age=27) The lambda accepts a Person and returns a Boolean. Collection functions call it for each element, then use the result for their specific job. section 6.1.3
Choose the collection operation whose name matches the question:
- all checks whether every element satisfies the predicate. section 6.1.3
- any checks whether at least one element satisfies it; none checks whether no element satisfies it. section 6.1.3
- count returns the number of matching elements, while find returns the first matching element. section 6.1.3
- For readability, prefer any with a negated condition over !all, and none over !any. section 6.1.3
- On an empty collection, any returns false, none returns true, and all returns true. section 6.1.3
Predicates are function values, so higher-order functions can accept them as parameters or return them. A filter implementation calls predicate(char) for each character and keeps only characters for which the result is true. A UI filter can likewise construct a predicate from prefix and phone-number settings, separating filtering policy from display logic. section 10.1.2 section 10.1.5
val canBeInClub27 = { p: Person -> p.age <= 27 }
Inspect the compact Kotlin predicate definition used to query people by age. section 6.1.3




