added a partition helper method to partition a collection into a collection of matching items and not matching items

This commit is contained in:
James Strachan
2012-09-12 15:55:34 +01:00
parent 6c4102bf23
commit ba01576c41
12 changed files with 206 additions and 0 deletions
+18
View File
@@ -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*
*