added a partition helper method to partition a collection into a collection of matching items and not matching items
This commit is contained in:
@@ -84,6 +84,24 @@ public inline fun <T, C: MutableCollection<in T>> Array<T>.filterTo(result: C, p
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt partition
|
||||
*/
|
||||
public inline fun <T> Array<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*
|
||||
*
|
||||
|
||||
@@ -84,6 +84,24 @@ public inline fun <C: MutableCollection<Boolean>> BooleanArray.filterTo(result:
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt partition
|
||||
*/
|
||||
public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean) : Pair<List<Boolean>, List<Boolean>> {
|
||||
val first = ArrayList<Boolean>()
|
||||
val second = ArrayList<Boolean>()
|
||||
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*
|
||||
*
|
||||
|
||||
@@ -84,6 +84,24 @@ public inline fun <C: MutableCollection<Byte>> ByteArray.filterTo(result: C, pre
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt partition
|
||||
*/
|
||||
public inline fun ByteArray.partition(predicate: (Byte) -> Boolean) : Pair<List<Byte>, List<Byte>> {
|
||||
val first = ArrayList<Byte>()
|
||||
val second = ArrayList<Byte>()
|
||||
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*
|
||||
*
|
||||
|
||||
@@ -84,6 +84,24 @@ public inline fun <C: MutableCollection<Char>> CharArray.filterTo(result: C, pre
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt partition
|
||||
*/
|
||||
public inline fun CharArray.partition(predicate: (Char) -> Boolean) : Pair<List<Char>, List<Char>> {
|
||||
val first = ArrayList<Char>()
|
||||
val second = ArrayList<Char>()
|
||||
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*
|
||||
*
|
||||
|
||||
@@ -84,6 +84,24 @@ public inline fun <C: MutableCollection<Double>> DoubleArray.filterTo(result: C,
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt partition
|
||||
*/
|
||||
public inline fun DoubleArray.partition(predicate: (Double) -> Boolean) : Pair<List<Double>, List<Double>> {
|
||||
val first = ArrayList<Double>()
|
||||
val second = ArrayList<Double>()
|
||||
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*
|
||||
*
|
||||
|
||||
@@ -84,6 +84,24 @@ public inline fun <C: MutableCollection<Float>> FloatArray.filterTo(result: C, p
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt partition
|
||||
*/
|
||||
public inline fun FloatArray.partition(predicate: (Float) -> Boolean) : Pair<List<Float>, List<Float>> {
|
||||
val first = ArrayList<Float>()
|
||||
val second = ArrayList<Float>()
|
||||
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*
|
||||
*
|
||||
|
||||
@@ -84,6 +84,24 @@ public inline fun <C: MutableCollection<Int>> IntArray.filterTo(result: C, predi
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt partition
|
||||
*/
|
||||
public inline fun IntArray.partition(predicate: (Int) -> Boolean) : Pair<List<Int>, List<Int>> {
|
||||
val first = ArrayList<Int>()
|
||||
val second = ArrayList<Int>()
|
||||
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*
|
||||
*
|
||||
|
||||
@@ -83,6 +83,24 @@ public inline fun <T, C: MutableCollection<in T>> Iterator<T>.filterTo(result: C
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt partition
|
||||
*/
|
||||
public inline fun <T> Iterator<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*
|
||||
*
|
||||
|
||||
@@ -84,6 +84,24 @@ public inline fun <C: MutableCollection<Long>> LongArray.filterTo(result: C, pre
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt partition
|
||||
*/
|
||||
public inline fun LongArray.partition(predicate: (Long) -> Boolean) : Pair<List<Long>, List<Long>> {
|
||||
val first = ArrayList<Long>()
|
||||
val second = ArrayList<Long>()
|
||||
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*
|
||||
*
|
||||
|
||||
@@ -84,6 +84,24 @@ public inline fun <C: MutableCollection<Short>> ShortArray.filterTo(result: C, p
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt partition
|
||||
*/
|
||||
public inline fun ShortArray.partition(predicate: (Short) -> Boolean) : Pair<List<Short>, List<Short>> {
|
||||
val first = ArrayList<Short>()
|
||||
val second = ArrayList<Short>()
|
||||
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*
|
||||
*
|
||||
|
||||
@@ -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*
|
||||
*
|
||||
|
||||
@@ -156,6 +156,14 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
test fun partition() {
|
||||
val data = arrayList("foo", "bar", "something", "xyz")
|
||||
val pair = data.partition{it.size == 3}
|
||||
|
||||
assertEquals(arrayList("foo", "bar", "xyz"), pair.first, "pair.first")
|
||||
assertEquals(arrayList("something"), pair.second, "pair.second")
|
||||
}
|
||||
|
||||
test fun reduce() {
|
||||
expect("1234") {
|
||||
val list = arrayList("1", "2", "3", "4")
|
||||
|
||||
Reference in New Issue
Block a user