Skip to content

Commit

Permalink
Add samples for partition
Browse files Browse the repository at this point in the history
(cherry picked from commit 7bb7c86)
  • Loading branch information
nsuriadi authored and ilya-g committed Apr 10, 2020
1 parent 48a6025 commit ed3479b
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 0 deletions.
18 changes: 18 additions & 0 deletions libraries/stdlib/common/src/generated/_Arrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14687,6 +14687,8 @@ public fun <T : Any> Array<T?>.requireNoNulls(): Array<T> {
* 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 <T> Array<out T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> {
val first = ArrayList<T>()
Expand All @@ -14705,6 +14707,8 @@ public inline fun <T> Array<out T>.partition(predicate: (T) -> Boolean): Pair<Li
* 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 ByteArray.partition(predicate: (Byte) -> Boolean): Pair<List<Byte>, List<Byte>> {
val first = ArrayList<Byte>()
Expand All @@ -14723,6 +14727,8 @@ public inline fun ByteArray.partition(predicate: (Byte) -> Boolean): Pair<List<B
* 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 ShortArray.partition(predicate: (Short) -> Boolean): Pair<List<Short>, List<Short>> {
val first = ArrayList<Short>()
Expand All @@ -14741,6 +14747,8 @@ public inline fun ShortArray.partition(predicate: (Short) -> Boolean): Pair<List
* 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 IntArray.partition(predicate: (Int) -> Boolean): Pair<List<Int>, List<Int>> {
val first = ArrayList<Int>()
Expand All @@ -14759,6 +14767,8 @@ public inline fun IntArray.partition(predicate: (Int) -> Boolean): Pair<List<Int
* 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 LongArray.partition(predicate: (Long) -> Boolean): Pair<List<Long>, List<Long>> {
val first = ArrayList<Long>()
Expand All @@ -14777,6 +14787,8 @@ public inline fun LongArray.partition(predicate: (Long) -> Boolean): Pair<List<L
* 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 FloatArray.partition(predicate: (Float) -> Boolean): Pair<List<Float>, List<Float>> {
val first = ArrayList<Float>()
Expand All @@ -14795,6 +14807,8 @@ public inline fun FloatArray.partition(predicate: (Float) -> Boolean): Pair<List
* 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 DoubleArray.partition(predicate: (Double) -> Boolean): Pair<List<Double>, List<Double>> {
val first = ArrayList<Double>()
Expand All @@ -14813,6 +14827,8 @@ public inline fun DoubleArray.partition(predicate: (Double) -> Boolean): Pair<Li
* 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 BooleanArray.partition(predicate: (Boolean) -> Boolean): Pair<List<Boolean>, List<Boolean>> {
val first = ArrayList<Boolean>()
Expand All @@ -14831,6 +14847,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<Char>, List<Char>> {
val first = ArrayList<Char>()
Expand Down
2 changes: 2 additions & 0 deletions libraries/stdlib/common/src/generated/_Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2212,6 +2212,8 @@ public inline fun <T> Iterable<T>.minusElement(element: T): List<T> {
* 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 <T> Iterable<T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> {
val first = ArrayList<T>()
Expand Down
2 changes: 2 additions & 0 deletions libraries/stdlib/common/src/generated/_Sequences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,8 @@ public inline fun <T> Sequence<T>.minusElement(element: T): Sequence<T> {
* while *second* list contains elements for which [predicate] yielded `false`.
*
* 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>> {
val first = ArrayList<T>()
Expand Down
4 changes: 4 additions & 0 deletions libraries/stdlib/common/src/generated/_Strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,8 @@ public fun <R> 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<CharSequence, CharSequence> {
val first = StringBuilder()
Expand All @@ -1504,6 +1506,8 @@ public inline fun CharSequence.partition(predicate: (Char) -> Boolean): Pair<Cha
* Splits the original string into pair of strings,
* where *first* string contains characters for which [predicate] yielded `true`,
* 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> {
val first = StringBuilder()
Expand Down
7 changes: 7 additions & 0 deletions libraries/stdlib/samples/test/samples/collections/arrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions libraries/stdlib/samples/test/samples/collections/iterables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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])")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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])")
}
}

}
7 changes: 7 additions & 0 deletions libraries/stdlib/samples/test/samples/text/strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>, List<T>>")
body {
Expand Down

0 comments on commit ed3479b

Please sign in to comment.