🐦 Kotlin
Intermediate
What is the partition function in Kotlin?
Answer
partition splits a collection into a Pair of two lists — the first list contains elements that satisfy the predicate and the second contains elements that do not. Example: val (adults, minors) = users.partition { it.age >= 18 }. This is more efficient than calling filter twice (once for the matching elements and once for the non-matching) because it iterates the collection only once. Destructuring the resulting pair directly into two variables makes the intent very readable.