From 7bb7c86fa994535a4fe834724d265345b82f5915 Mon Sep 17 00:00:00 2001 From: n-p-s <6024770+n-p-s@users.noreply.github.com> Date: Sat, 25 Jan 2020 17:06:02 +1100 Subject: [PATCH] Add samples for partition --- .../stdlib/common/src/generated/_Arrays.kt | 18 ++++++++++++++++++ .../common/src/generated/_Collections.kt | 2 ++ .../stdlib/common/src/generated/_Sequences.kt | 2 ++ .../stdlib/common/src/generated/_Strings.kt | 4 ++++ .../samples/test/samples/collections/arrays.kt | 7 +++++++ .../test/samples/collections/iterables.kt | 13 +++++++++++++ .../test/samples/collections/sequences.kt | 7 +++++++ .../samples/test/samples/text/strings.kt | 7 +++++++ .../src/templates/Generators.kt | 6 ++++++ 9 files changed, 66 insertions(+) diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 62d076ba049cb..035a2eabe42bc 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -15662,6 +15662,8 @@ public fun Array.requireNoNulls(): Array { * 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`. + * + * @sample samples.collections.Arrays.Transformations.partitionArrayOfPrimitives */ public inline fun Array.partition(predicate: (T) -> Boolean): Pair, List> { val first = ArrayList() @@ -15680,6 +15682,8 @@ public inline fun Array.partition(predicate: (T) -> Boolean): Pair
  • Boolean): Pair, List> { val first = ArrayList() @@ -15698,6 +15702,8 @@ public inline fun ByteArray.partition(predicate: (Byte) -> Boolean): Pair Boolean): Pair, List> { val first = ArrayList() @@ -15716,6 +15722,8 @@ public inline fun ShortArray.partition(predicate: (Short) -> Boolean): Pair Boolean): Pair, List> { val first = ArrayList() @@ -15734,6 +15742,8 @@ public inline fun IntArray.partition(predicate: (Int) -> Boolean): Pair Boolean): Pair, List> { val first = ArrayList() @@ -15752,6 +15762,8 @@ public inline fun LongArray.partition(predicate: (Long) -> Boolean): Pair Boolean): Pair, List> { val first = ArrayList() @@ -15770,6 +15782,8 @@ public inline fun FloatArray.partition(predicate: (Float) -> Boolean): Pair Boolean): Pair, List> { val first = ArrayList() @@ -15788,6 +15802,8 @@ public inline fun DoubleArray.partition(predicate: (Double) -> Boolean): Pair
  • Boolean): Pair, List> { val first = ArrayList() @@ -15806,6 +15822,8 @@ public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean): Pair< * 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`. + * + * @sample samples.collections.Arrays.Transformations.partitionArrayOfPrimitives */ public inline fun CharArray.partition(predicate: (Char) -> Boolean): Pair, List> { val first = ArrayList() diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index 8719d232b748c..e44f17614ed43 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -2257,6 +2257,8 @@ public inline fun Iterable.minusElement(element: T): List { * 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`. + * + * @sample samples.collections.Iterables.Operations.partition */ public inline fun Iterable.partition(predicate: (T) -> Boolean): Pair, List> { val first = ArrayList() diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index a02db375f1077..03fc73d1bde28 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -1723,6 +1723,8 @@ public inline fun Sequence.minusElement(element: T): Sequence { * while *second* list contains elements for which [predicate] yielded `false`. * * The operation is _terminal_. + * + * @sample samples.collections.Sequences.Transformations.partition */ public inline fun Sequence.partition(predicate: (T) -> Boolean): Pair, List> { val first = ArrayList() diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index 665fe4f8fdf0a..f3db92dd03e70 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -1527,6 +1527,8 @@ public fun CharSequence.chunkedSequence(size: Int, transform: (CharSequence) * Splits the original char sequence into pair of char sequences, * where *first* char sequence contains characters for which [predicate] yielded `true`, * 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 { val first = StringBuilder() @@ -1545,6 +1547,8 @@ public inline fun CharSequence.partition(predicate: (Char) -> Boolean): Pair Boolean): Pair { val first = StringBuilder() diff --git a/libraries/stdlib/samples/test/samples/collections/arrays.kt b/libraries/stdlib/samples/test/samples/collections/arrays.kt index 0b737d366c3d4..02ab1d4f27e75 100644 --- a/libraries/stdlib/samples/test/samples/collections/arrays.kt +++ b/libraries/stdlib/samples/test/samples/collections/arrays.kt @@ -146,6 +146,13 @@ class Arrays { val array = arrayOf(1 to 'a', 2 to 'b', 3 to '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 { diff --git a/libraries/stdlib/samples/test/samples/collections/iterables.kt b/libraries/stdlib/samples/test/samples/collections/iterables.kt index e1e308964dc63..63037a3791174 100644 --- a/libraries/stdlib/samples/test/samples/collections/iterables.kt +++ b/libraries/stdlib/samples/test/samples/collections/iterables.kt @@ -71,5 +71,18 @@ class Iterables { val result = listA.zip(listB) { a, b -> "$a$b" } 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])") + } } } \ No newline at end of file diff --git a/libraries/stdlib/samples/test/samples/collections/sequences.kt b/libraries/stdlib/samples/test/samples/collections/sequences.kt index 5618648a22e1f..bdda680da3f6e 100644 --- a/libraries/stdlib/samples/test/samples/collections/sequences.kt +++ b/libraries/stdlib/samples/test/samples/collections/sequences.kt @@ -228,6 +228,13 @@ class Sequences { val result = sequenceA.zip(sequenceB) { a, b -> "$a/$b" } 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])") + } } } \ No newline at end of file diff --git a/libraries/stdlib/samples/test/samples/text/strings.kt b/libraries/stdlib/samples/test/samples/text/strings.kt index 2e33658afb51b..b6eb5052de068 100644 --- a/libraries/stdlib/samples/test/samples/text/strings.kt +++ b/libraries/stdlib/samples/test/samples/text/strings.kt @@ -170,6 +170,13 @@ class Strings { 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 fun stringToByteArray() { val charset = Charsets.UTF_8 diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index 1e644d07bdfd3..302760960c26c 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -580,6 +580,12 @@ object Generators : TemplateGroupBase() { 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) returns("Pair, List>") body {