added a partition helper method to partition a collection into a collection of matching items and not matching items
This commit is contained in:
@@ -75,6 +75,24 @@ public inline fun <T, C: MutableCollection<in T>> Iterable<T>.filterTo(result: C
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt partition
|
||||
*/
|
||||
public inline fun <T> Iterable<T>.partition(predicate: (T) -> Boolean) : Pair<List<T>, List<T>> {
|
||||
val first = ArrayList<T>()
|
||||
val second = ArrayList<T>()
|
||||
for (element in this) {
|
||||
if (predicate(element)) {
|
||||
first.add(element)
|
||||
} else {
|
||||
second.add(element)
|
||||
}
|
||||
}
|
||||
return Pair(first, second)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user