The 'partition' function in Kotlin allows you to split a collection into two separate lists based on a given predicate. This is particularly useful when you want to categorize items in a collection into two groups, such as filtering a list of numbers into evens and odds or separating valid and invalid entries.
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val (evens, odds) = numbers.partition { it % 2 == 0 } println("Evens: $evens") // Output: Evens: [2, 4, 6, 8, 10] println("Odds: $odds") // Output: Odds: [1, 3, 5, 7, 9]