diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index fcaf9e61d67..13a2f4fdfbb 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -9640,6 +9640,8 @@ public fun CharArray.withIndex(): Iterable> { * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun Array.distinct(): List { return this.toMutableSet().toList() @@ -9649,6 +9651,8 @@ public fun Array.distinct(): List { * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun ByteArray.distinct(): List { return this.toMutableSet().toList() @@ -9658,6 +9662,8 @@ public fun ByteArray.distinct(): List { * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun ShortArray.distinct(): List { return this.toMutableSet().toList() @@ -9667,6 +9673,8 @@ public fun ShortArray.distinct(): List { * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun IntArray.distinct(): List { return this.toMutableSet().toList() @@ -9676,6 +9684,8 @@ public fun IntArray.distinct(): List { * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun LongArray.distinct(): List { return this.toMutableSet().toList() @@ -9685,6 +9695,8 @@ public fun LongArray.distinct(): List { * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun FloatArray.distinct(): List { return this.toMutableSet().toList() @@ -9694,6 +9706,8 @@ public fun FloatArray.distinct(): List { * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun DoubleArray.distinct(): List { return this.toMutableSet().toList() @@ -9703,6 +9717,8 @@ public fun DoubleArray.distinct(): List { * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun BooleanArray.distinct(): List { return this.toMutableSet().toList() @@ -9712,6 +9728,8 @@ public fun BooleanArray.distinct(): List { * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun CharArray.distinct(): List { return this.toMutableSet().toList() @@ -9722,6 +9740,8 @@ public fun CharArray.distinct(): List { * having distinct keys returned by the given [selector] function. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public inline fun Array.distinctBy(selector: (T) -> K): List { val set = HashSet() @@ -9739,6 +9759,8 @@ public inline fun Array.distinctBy(selector: (T) -> K): List { * having distinct keys returned by the given [selector] function. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public inline fun ByteArray.distinctBy(selector: (Byte) -> K): List { val set = HashSet() @@ -9756,6 +9778,8 @@ public inline fun ByteArray.distinctBy(selector: (Byte) -> K): List { * having distinct keys returned by the given [selector] function. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public inline fun ShortArray.distinctBy(selector: (Short) -> K): List { val set = HashSet() @@ -9773,6 +9797,8 @@ public inline fun ShortArray.distinctBy(selector: (Short) -> K): List * having distinct keys returned by the given [selector] function. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public inline fun IntArray.distinctBy(selector: (Int) -> K): List { val set = HashSet() @@ -9790,6 +9816,8 @@ public inline fun IntArray.distinctBy(selector: (Int) -> K): List { * having distinct keys returned by the given [selector] function. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public inline fun LongArray.distinctBy(selector: (Long) -> K): List { val set = HashSet() @@ -9807,6 +9835,8 @@ public inline fun LongArray.distinctBy(selector: (Long) -> K): List { * having distinct keys returned by the given [selector] function. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public inline fun FloatArray.distinctBy(selector: (Float) -> K): List { val set = HashSet() @@ -9824,6 +9854,8 @@ public inline fun FloatArray.distinctBy(selector: (Float) -> K): List * having distinct keys returned by the given [selector] function. * * The elements in the resulting list are in the same order as they were in the source array. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public inline fun DoubleArray.distinctBy(selector: (Double) -> K): List { val set = HashSet() @@ -9841,6 +9873,8 @@ public inline fun DoubleArray.distinctBy(selector: (Double) -> K): List BooleanArray.distinctBy(selector: (Boolean) -> K): List { val set = HashSet() @@ -9858,6 +9892,8 @@ public inline fun BooleanArray.distinctBy(selector: (Boolean) -> K): List CharArray.distinctBy(selector: (Char) -> K): List { val set = HashSet() diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index e5d0346ede0..a28298d269c 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -1422,6 +1422,8 @@ public fun Iterable.withIndex(): Iterable> { * Returns a list containing only distinct elements from the given collection. * * The elements in the resulting list are in the same order as they were in the source collection. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun Iterable.distinct(): List { return this.toMutableSet().toList() @@ -1432,6 +1434,8 @@ public fun Iterable.distinct(): List { * having distinct keys returned by the given [selector] function. * * The elements in the resulting list are in the same order as they were in the source collection. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public inline fun Iterable.distinctBy(selector: (T) -> K): List { val set = HashSet() diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index 5900d100a86..78826302169 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -978,6 +978,8 @@ public fun Sequence.withIndex(): Sequence> { * The elements in the resulting sequence are in the same order as they were in the source sequence. * * The operation is _intermediate_ and _stateful_. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun Sequence.distinct(): Sequence { return this.distinctBy { it } @@ -990,6 +992,8 @@ public fun Sequence.distinct(): Sequence { * The elements in the resulting sequence are in the same order as they were in the source sequence. * * The operation is _intermediate_ and _stateful_. + * + * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy */ public fun Sequence.distinctBy(selector: (T) -> K): Sequence { return DistinctSequence(this, selector) diff --git a/libraries/stdlib/samples/test/samples/collections/collections.kt b/libraries/stdlib/samples/test/samples/collections/collections.kt index be010e197b7..5446428c492 100644 --- a/libraries/stdlib/samples/test/samples/collections/collections.kt +++ b/libraries/stdlib/samples/test/samples/collections/collections.kt @@ -448,6 +448,13 @@ class Collections { assertPrints(withLengthOfNames, "{Grace Hopper=11, Jacob Bernoulli=14}") } + @Sample + fun distinctAndDistinctBy() { + val list = listOf('a', 'A', 'b', 'B', 'A', 'a') + assertPrints(list.distinct(), "[a, A, b, B]") + assertPrints(list.distinctBy { it.toUpperCase() }, "[a, b]") + } + @Sample fun groupBy() { val words = listOf("a", "abc", "ab", "def", "abcd") diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt index 82d0b03f072..14f4f3713ab 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt @@ -64,6 +64,7 @@ object SetOps : TemplateGroupBase() { returns("Sequence") body { "return this.distinctBy { it }" } } + sample("samples.collections.Collections.Transformations.distinctAndDistinctBy") } val f_distinctBy = fn("distinctBy(selector: (T) -> K)") { @@ -100,6 +101,8 @@ object SetOps : TemplateGroupBase() { sequenceClassification(intermediate, stateful) body { """return DistinctSequence(this, selector)""" } } + + sample("samples.collections.Collections.Transformations.distinctAndDistinctBy") } val f_union = fn("union(other: Iterable)") {