partition

inline fun <T> Array<out T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>>(source)
inline fun ByteArray.partition(predicate: (Byte) -> Boolean): Pair<List<Byte>, List<Byte>>(source)
inline fun ShortArray.partition(predicate: (Short) -> Boolean): Pair<List<Short>, List<Short>>(source)
inline fun IntArray.partition(predicate: (Int) -> Boolean): Pair<List<Int>, List<Int>>(source)
inline fun LongArray.partition(predicate: (Long) -> Boolean): Pair<List<Long>, List<Long>>(source)
inline fun FloatArray.partition(predicate: (Float) -> Boolean): Pair<List<Float>, List<Float>>(source)
inline fun DoubleArray.partition(predicate: (Double) -> Boolean): Pair<List<Double>, List<Double>>(source)
inline fun CharArray.partition(predicate: (Char) -> Boolean): Pair<List<Char>, List<Char>>(source)

Splits the original array into pair of lists, where first list contains elements for which predicate yielded true, while second list contains elements for which predicate yielded false.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val array = intArrayOf(1, 2, 3, 4, 5)
val (even, odd) = array.partition { it % 2 == 0 }
println(even) // [2, 4]
println(odd) // [1, 3, 5] 
   //sampleEnd
}

inline fun <T> Iterable<T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>>(source)

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.

Since Kotlin

1.0

Samples


fun main() { 
   //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
}