Add samples for partition

This commit is contained in:
n-p-s
2020-01-25 17:06:02 +11:00
committed by Ilya Gorbunov
parent dd2aa3de28
commit 7bb7c86fa9
9 changed files with 66 additions and 0 deletions
@@ -15662,6 +15662,8 @@ public fun <T : Any> Array<T?>.requireNoNulls(): Array<T> {
* Splits the original array into pair of lists, * Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`, * where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`. * while *second* list contains elements for which [predicate] yielded `false`.
*
* @sample samples.collections.Arrays.Transformations.partitionArrayOfPrimitives
*/ */
public inline fun <T> Array<out T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> { public inline fun <T> Array<out T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> {
val first = ArrayList<T>() val first = ArrayList<T>()
@@ -15680,6 +15682,8 @@ public inline fun <T> Array<out T>.partition(predicate: (T) -> Boolean): Pair<Li
* Splits the original array into pair of lists, * Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`, * where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`. * while *second* list contains elements for which [predicate] yielded `false`.
*
* @sample samples.collections.Arrays.Transformations.partitionArrayOfPrimitives
*/ */
public inline fun ByteArray.partition(predicate: (Byte) -> Boolean): Pair<List<Byte>, List<Byte>> { public inline fun ByteArray.partition(predicate: (Byte) -> Boolean): Pair<List<Byte>, List<Byte>> {
val first = ArrayList<Byte>() val first = ArrayList<Byte>()
@@ -15698,6 +15702,8 @@ public inline fun ByteArray.partition(predicate: (Byte) -> Boolean): Pair<List<B
* Splits the original array into pair of lists, * Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`, * where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`. * while *second* list contains elements for which [predicate] yielded `false`.
*
* @sample samples.collections.Arrays.Transformations.partitionArrayOfPrimitives
*/ */
public inline fun ShortArray.partition(predicate: (Short) -> Boolean): Pair<List<Short>, List<Short>> { public inline fun ShortArray.partition(predicate: (Short) -> Boolean): Pair<List<Short>, List<Short>> {
val first = ArrayList<Short>() val first = ArrayList<Short>()
@@ -15716,6 +15722,8 @@ public inline fun ShortArray.partition(predicate: (Short) -> Boolean): Pair<List
* Splits the original array into pair of lists, * Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`, * where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`. * while *second* list contains elements for which [predicate] yielded `false`.
*
* @sample samples.collections.Arrays.Transformations.partitionArrayOfPrimitives
*/ */
public inline fun IntArray.partition(predicate: (Int) -> Boolean): Pair<List<Int>, List<Int>> { public inline fun IntArray.partition(predicate: (Int) -> Boolean): Pair<List<Int>, List<Int>> {
val first = ArrayList<Int>() val first = ArrayList<Int>()
@@ -15734,6 +15742,8 @@ public inline fun IntArray.partition(predicate: (Int) -> Boolean): Pair<List<Int
* Splits the original array into pair of lists, * Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`, * where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`. * while *second* list contains elements for which [predicate] yielded `false`.
*
* @sample samples.collections.Arrays.Transformations.partitionArrayOfPrimitives
*/ */
public inline fun LongArray.partition(predicate: (Long) -> Boolean): Pair<List<Long>, List<Long>> { public inline fun LongArray.partition(predicate: (Long) -> Boolean): Pair<List<Long>, List<Long>> {
val first = ArrayList<Long>() val first = ArrayList<Long>()
@@ -15752,6 +15762,8 @@ public inline fun LongArray.partition(predicate: (Long) -> Boolean): Pair<List<L
* Splits the original array into pair of lists, * Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`, * where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`. * while *second* list contains elements for which [predicate] yielded `false`.
*
* @sample samples.collections.Arrays.Transformations.partitionArrayOfPrimitives
*/ */
public inline fun FloatArray.partition(predicate: (Float) -> Boolean): Pair<List<Float>, List<Float>> { public inline fun FloatArray.partition(predicate: (Float) -> Boolean): Pair<List<Float>, List<Float>> {
val first = ArrayList<Float>() val first = ArrayList<Float>()
@@ -15770,6 +15782,8 @@ public inline fun FloatArray.partition(predicate: (Float) -> Boolean): Pair<List
* Splits the original array into pair of lists, * Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`, * where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`. * while *second* list contains elements for which [predicate] yielded `false`.
*
* @sample samples.collections.Arrays.Transformations.partitionArrayOfPrimitives
*/ */
public inline fun DoubleArray.partition(predicate: (Double) -> Boolean): Pair<List<Double>, List<Double>> { public inline fun DoubleArray.partition(predicate: (Double) -> Boolean): Pair<List<Double>, List<Double>> {
val first = ArrayList<Double>() val first = ArrayList<Double>()
@@ -15788,6 +15802,8 @@ public inline fun DoubleArray.partition(predicate: (Double) -> Boolean): Pair<Li
* Splits the original array into pair of lists, * Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`, * where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`. * while *second* list contains elements for which [predicate] yielded `false`.
*
* @sample samples.collections.Arrays.Transformations.partitionArrayOfPrimitives
*/ */
public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean): Pair<List<Boolean>, List<Boolean>> { public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean): Pair<List<Boolean>, List<Boolean>> {
val first = ArrayList<Boolean>() val first = ArrayList<Boolean>()
@@ -15806,6 +15822,8 @@ public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean): Pair<
* Splits the original array into pair of lists, * Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`, * where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`. * while *second* list contains elements for which [predicate] yielded `false`.
*
* @sample samples.collections.Arrays.Transformations.partitionArrayOfPrimitives
*/ */
public inline fun CharArray.partition(predicate: (Char) -> Boolean): Pair<List<Char>, List<Char>> { public inline fun CharArray.partition(predicate: (Char) -> Boolean): Pair<List<Char>, List<Char>> {
val first = ArrayList<Char>() val first = ArrayList<Char>()
@@ -2257,6 +2257,8 @@ public inline fun <T> Iterable<T>.minusElement(element: T): List<T> {
* Splits the original collection into pair of lists, * Splits the original collection into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`, * where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`. * while *second* list contains elements for which [predicate] yielded `false`.
*
* @sample samples.collections.Iterables.Operations.partition
*/ */
public inline fun <T> Iterable<T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> { public inline fun <T> Iterable<T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> {
val first = ArrayList<T>() val first = ArrayList<T>()
@@ -1723,6 +1723,8 @@ public inline fun <T> Sequence<T>.minusElement(element: T): Sequence<T> {
* while *second* list contains elements for which [predicate] yielded `false`. * while *second* list contains elements for which [predicate] yielded `false`.
* *
* The operation is _terminal_. * The operation is _terminal_.
*
* @sample samples.collections.Sequences.Transformations.partition
*/ */
public inline fun <T> Sequence<T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> { public inline fun <T> Sequence<T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> {
val first = ArrayList<T>() val first = ArrayList<T>()
@@ -1527,6 +1527,8 @@ public fun <R> CharSequence.chunkedSequence(size: Int, transform: (CharSequence)
* Splits the original char sequence into pair of char sequences, * Splits the original char sequence into pair of char sequences,
* where *first* char sequence contains characters for which [predicate] yielded `true`, * where *first* char sequence contains characters for which [predicate] yielded `true`,
* while *second* char sequence contains characters for which [predicate] yielded `false`. * while *second* char sequence contains characters for which [predicate] yielded `false`.
*
* @sample samples.text.Strings.partition
*/ */
public inline fun CharSequence.partition(predicate: (Char) -> Boolean): Pair<CharSequence, CharSequence> { public inline fun CharSequence.partition(predicate: (Char) -> Boolean): Pair<CharSequence, CharSequence> {
val first = StringBuilder() val first = StringBuilder()
@@ -1545,6 +1547,8 @@ public inline fun CharSequence.partition(predicate: (Char) -> Boolean): Pair<Cha
* Splits the original string into pair of strings, * Splits the original string into pair of strings,
* where *first* string contains characters for which [predicate] yielded `true`, * where *first* string contains characters for which [predicate] yielded `true`,
* while *second* string contains characters for which [predicate] yielded `false`. * while *second* string contains characters for which [predicate] yielded `false`.
*
* @sample samples.text.Strings.partition
*/ */
public inline fun String.partition(predicate: (Char) -> Boolean): Pair<String, String> { public inline fun String.partition(predicate: (Char) -> Boolean): Pair<String, String> {
val first = StringBuilder() val first = StringBuilder()
@@ -146,6 +146,13 @@ class Arrays {
val array = arrayOf(1 to 'a', 2 to 'b', 3 to 'c') val array = arrayOf(1 to 'a', 2 to 'b', 3 to 'c')
assertPrints(array.unzip(), "([1, 2, 3], [a, b, c])") assertPrints(array.unzip(), "([1, 2, 3], [a, b, c])")
} }
@Sample
fun partitionArrayOfPrimitives() {
val array = intArrayOf(1, 2, 3, 4, 5)
val partition = array.partition { it % 2 == 0 }
assertPrints(partition, "([2, 4], [1, 3, 5])")
}
} }
class ContentOperations { class ContentOperations {
@@ -71,5 +71,18 @@ class Iterables {
val result = listA.zip(listB) { a, b -> "$a$b" } val result = listA.zip(listB) { a, b -> "$a$b" }
assertPrints(result, "[a1, b2, c3]") assertPrints(result, "[a1, b2, c3]")
} }
@Sample
fun partition() {
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 }
assertPrints(result, "([Tom - 18, Sarah - 22], [Andy - 32])")
}
} }
} }
@@ -228,6 +228,13 @@ class Sequences {
val result = sequenceA.zip(sequenceB) { a, b -> "$a/$b" } val result = sequenceA.zip(sequenceB) { a, b -> "$a/$b" }
assertPrints(result.take(4).toList(), "[a/1, b/3, c/7, d/15]") assertPrints(result.take(4).toList(), "[a/1, b/3, c/7, d/15]")
} }
@Sample
fun partition() {
val sequence = sequenceOf(1, 2, 3, 4, 5)
val result = sequence.partition { it % 2 == 0 }
assertPrints(result, "([2, 4], [1, 3, 5])")
}
} }
} }
@@ -170,6 +170,13 @@ class Strings {
assertPrints(result, "{b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}") assertPrints(result, "{b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}")
} }
@Sample
fun partition() {
val string = "Hello"
val result = string.partition { it == 'l' }
assertPrints(result, "(ll, Heo)")
}
@Sample @Sample
fun stringToByteArray() { fun stringToByteArray() {
val charset = Charsets.UTF_8 val charset = Charsets.UTF_8
@@ -580,6 +580,12 @@ object Generators : TemplateGroupBase() {
while *second* list contains elements for which [predicate] yielded `false`. while *second* list contains elements for which [predicate] yielded `false`.
""" """
} }
sample(when (family) {
CharSequences, Strings -> "samples.text.Strings.partition"
ArraysOfObjects, ArraysOfPrimitives -> "samples.collections.Arrays.Transformations.partitionArrayOfPrimitives"
Sequences -> "samples.collections.Sequences.Transformations.partition"
else -> "samples.collections.Iterables.Operations.partition"
})
sequenceClassification(terminal) sequenceClassification(terminal)
returns("Pair<List<T>, List<T>>") returns("Pair<List<T>, List<T>>")
body { body {