inline fun DoubleArray.partition( predicate: (Double) -> Boolean ): Pair<List<Double>, List<Double>>
inline fun BooleanArray.partition( predicate: (Boolean) -> Boolean ): Pair<List<Boolean>, List<Boolean>>
Splits the original collection into pair of lists, where first list contains elements for which predicate yielded true
, while second list contains elements for which predicate yielded false
.
fun main(args: Array<String>) {
//sampleStart
data class Person(val name: String, val age: Int) {
override fun toString(): String {
return "$name - $age"
}
}
val list = listOf(Person("Tom", 18), Person("Andy", 32), Person("Sarah", 22))
val result = list.partition { it.age < 30 }
println(result) // ([Tom - 18, Sarah - 22], [Andy - 32])
//sampleEnd
}
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/partition.html