From f334ad2ffc26be755d9752da9993af2f40292143 Mon Sep 17 00:00:00 2001 From: Dat Trieu Date: Fri, 29 Nov 2019 14:54:02 +0100 Subject: [PATCH] KT-20357: Add samples for associate* functions (#2798) * KT-20357: Add samples for functions related to associate * KT-20357: Use same fib function for both samples * KT-20357: Use examples with duplicate keys and simplify examples with primitives * KT-20357: Use primitive samples for all arrays * KT-20357: Use String splitting example to better illustrate the use for associate over associateBy with a valueSelector --- .../stdlib/common/src/generated/_Arrays.kt | 108 ++++++++++++++++++ .../common/src/generated/_Collections.kt | 14 +++ .../stdlib/common/src/generated/_Sequences.kt | 14 +++ .../stdlib/common/src/generated/_Strings.kt | 14 +++ .../test/samples/collections/arrays.kt | 66 +++++++++++ .../test/samples/collections/collections.kt | 103 +++++++++++++++++ .../samples/test/samples/text/strings.kt | 67 +++++++++++ .../src/templates/Snapshots.kt | 34 ++++++ 8 files changed, 420 insertions(+) diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 1f448740d26..499042bd0e8 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -7145,6 +7145,8 @@ public expect fun CharArray.toTypedArray(): Array * If any of two pairs would have the same key the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives */ public inline fun Array.associate(transform: (T) -> Pair): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7158,6 +7160,8 @@ public inline fun Array.associate(transform: (T) -> Pair) * If any of two pairs would have the same key the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives */ public inline fun ByteArray.associate(transform: (Byte) -> Pair): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7171,6 +7175,8 @@ public inline fun ByteArray.associate(transform: (Byte) -> Pair): M * If any of two pairs would have the same key the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives */ public inline fun ShortArray.associate(transform: (Short) -> Pair): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7184,6 +7190,8 @@ public inline fun ShortArray.associate(transform: (Short) -> Pair): * If any of two pairs would have the same key the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives */ public inline fun IntArray.associate(transform: (Int) -> Pair): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7197,6 +7205,8 @@ public inline fun IntArray.associate(transform: (Int) -> Pair): Map * If any of two pairs would have the same key the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives */ public inline fun LongArray.associate(transform: (Long) -> Pair): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7210,6 +7220,8 @@ public inline fun LongArray.associate(transform: (Long) -> Pair): M * If any of two pairs would have the same key the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives */ public inline fun FloatArray.associate(transform: (Float) -> Pair): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7223,6 +7235,8 @@ public inline fun FloatArray.associate(transform: (Float) -> Pair): * If any of two pairs would have the same key the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives */ public inline fun DoubleArray.associate(transform: (Double) -> Pair): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7236,6 +7250,8 @@ public inline fun DoubleArray.associate(transform: (Double) -> Pair * If any of two pairs would have the same key the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives */ public inline fun BooleanArray.associate(transform: (Boolean) -> Pair): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7249,6 +7265,8 @@ public inline fun BooleanArray.associate(transform: (Boolean) -> Pair CharArray.associate(transform: (Char) -> Pair): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7262,6 +7280,8 @@ public inline fun CharArray.associate(transform: (Char) -> Pair): M * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesBy */ public inline fun Array.associateBy(keySelector: (T) -> K): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7275,6 +7295,8 @@ public inline fun Array.associateBy(keySelector: (T) -> K): Map ByteArray.associateBy(keySelector: (Byte) -> K): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7288,6 +7310,8 @@ public inline fun ByteArray.associateBy(keySelector: (Byte) -> K): Map ShortArray.associateBy(keySelector: (Short) -> K): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7301,6 +7325,8 @@ public inline fun ShortArray.associateBy(keySelector: (Short) -> K): Map IntArray.associateBy(keySelector: (Int) -> K): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7314,6 +7340,8 @@ public inline fun IntArray.associateBy(keySelector: (Int) -> K): Map * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesBy */ public inline fun LongArray.associateBy(keySelector: (Long) -> K): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7327,6 +7355,8 @@ public inline fun LongArray.associateBy(keySelector: (Long) -> K): Map FloatArray.associateBy(keySelector: (Float) -> K): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7340,6 +7370,8 @@ public inline fun FloatArray.associateBy(keySelector: (Float) -> K): Map DoubleArray.associateBy(keySelector: (Double) -> K): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7353,6 +7385,8 @@ public inline fun DoubleArray.associateBy(keySelector: (Double) -> K): Map BooleanArray.associateBy(keySelector: (Boolean) -> K): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7366,6 +7400,8 @@ public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K): Map * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesBy */ public inline fun CharArray.associateBy(keySelector: (Char) -> K): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7378,6 +7414,8 @@ public inline fun CharArray.associateBy(keySelector: (Char) -> K): Map Array.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7390,6 +7428,8 @@ public inline fun Array.associateBy(keySelector: (T) -> K, valu * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform */ public inline fun ByteArray.associateBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7402,6 +7442,8 @@ public inline fun ByteArray.associateBy(keySelector: (Byte) -> K, valueTr * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform */ public inline fun ShortArray.associateBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7414,6 +7456,8 @@ public inline fun ShortArray.associateBy(keySelector: (Short) -> K, value * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform */ public inline fun IntArray.associateBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7426,6 +7470,8 @@ public inline fun IntArray.associateBy(keySelector: (Int) -> K, valueTran * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform */ public inline fun LongArray.associateBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7438,6 +7484,8 @@ public inline fun LongArray.associateBy(keySelector: (Long) -> K, valueTr * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform */ public inline fun FloatArray.associateBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7450,6 +7498,8 @@ public inline fun FloatArray.associateBy(keySelector: (Float) -> K, value * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform */ public inline fun DoubleArray.associateBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7462,6 +7512,8 @@ public inline fun DoubleArray.associateBy(keySelector: (Double) -> K, val * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform */ public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7474,6 +7526,8 @@ public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K, v * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original array. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform */ public inline fun CharArray.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map { val capacity = mapCapacity(size).coerceAtLeast(16) @@ -7486,6 +7540,8 @@ public inline fun CharArray.associateBy(keySelector: (Char) -> K, valueTr * and value is the element itself. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByTo */ public inline fun > Array.associateByTo(destination: M, keySelector: (T) -> K): M { for (element in this) { @@ -7500,6 +7556,8 @@ public inline fun > Array.associateByTo( * and value is the element itself. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByTo */ public inline fun > ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K): M { for (element in this) { @@ -7514,6 +7572,8 @@ public inline fun > ByteArray.associateByTo(des * and value is the element itself. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByTo */ public inline fun > ShortArray.associateByTo(destination: M, keySelector: (Short) -> K): M { for (element in this) { @@ -7528,6 +7588,8 @@ public inline fun > ShortArray.associateByTo(d * and value is the element itself. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByTo */ public inline fun > IntArray.associateByTo(destination: M, keySelector: (Int) -> K): M { for (element in this) { @@ -7542,6 +7604,8 @@ public inline fun > IntArray.associateByTo(desti * and value is the element itself. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByTo */ public inline fun > LongArray.associateByTo(destination: M, keySelector: (Long) -> K): M { for (element in this) { @@ -7556,6 +7620,8 @@ public inline fun > LongArray.associateByTo(des * and value is the element itself. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByTo */ public inline fun > FloatArray.associateByTo(destination: M, keySelector: (Float) -> K): M { for (element in this) { @@ -7570,6 +7636,8 @@ public inline fun > FloatArray.associateByTo(d * and value is the element itself. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByTo */ public inline fun > DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K): M { for (element in this) { @@ -7584,6 +7652,8 @@ public inline fun > DoubleArray.associateByTo * and value is the element itself. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByTo */ public inline fun > BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K): M { for (element in this) { @@ -7598,6 +7668,8 @@ public inline fun > BooleanArray.associateBy * and value is the element itself. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByTo */ public inline fun > CharArray.associateByTo(destination: M, keySelector: (Char) -> K): M { for (element in this) { @@ -7612,6 +7684,8 @@ public inline fun > CharArray.associateByTo(des * and value is provided by the [valueTransform] function applied to elements of the given array. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByToWithValueTransform */ public inline fun > Array.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { for (element in this) { @@ -7626,6 +7700,8 @@ public inline fun > Array.associateBy * and value is provided by the [valueTransform] function applied to elements of the given array. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByToWithValueTransform */ public inline fun > ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M { for (element in this) { @@ -7640,6 +7716,8 @@ public inline fun > ByteArray.associateByTo(des * and value is provided by the [valueTransform] function applied to elements of the given array. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByToWithValueTransform */ public inline fun > ShortArray.associateByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M { for (element in this) { @@ -7654,6 +7732,8 @@ public inline fun > ShortArray.associateByTo(de * and value is provided by the [valueTransform] function applied to elements of the given array. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByToWithValueTransform */ public inline fun > IntArray.associateByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M { for (element in this) { @@ -7668,6 +7748,8 @@ public inline fun > IntArray.associateByTo(dest * and value is provided by the [valueTransform] function applied to elements of the given array. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByToWithValueTransform */ public inline fun > LongArray.associateByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M { for (element in this) { @@ -7682,6 +7764,8 @@ public inline fun > LongArray.associateByTo(des * and value is provided by the [valueTransform] function applied to elements of the given array. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByToWithValueTransform */ public inline fun > FloatArray.associateByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M { for (element in this) { @@ -7696,6 +7780,8 @@ public inline fun > FloatArray.associateByTo(de * and value is provided by the [valueTransform] function applied to elements of the given array. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByToWithValueTransform */ public inline fun > DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M { for (element in this) { @@ -7710,6 +7796,8 @@ public inline fun > DoubleArray.associateByTo(d * and value is provided by the [valueTransform] function applied to elements of the given array. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByToWithValueTransform */ public inline fun > BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M { for (element in this) { @@ -7724,6 +7812,8 @@ public inline fun > BooleanArray.associateByTo( * and value is provided by the [valueTransform] function applied to elements of the given array. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByToWithValueTransform */ public inline fun > CharArray.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { for (element in this) { @@ -7737,6 +7827,8 @@ public inline fun > CharArray.associateByTo(des * provided by [transform] function applied to each element of the given array. * * If any of two pairs would have the same key the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesTo */ public inline fun > Array.associateTo(destination: M, transform: (T) -> Pair): M { for (element in this) { @@ -7750,6 +7842,8 @@ public inline fun > Array.associateTo * provided by [transform] function applied to each element of the given array. * * If any of two pairs would have the same key the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesTo */ public inline fun > ByteArray.associateTo(destination: M, transform: (Byte) -> Pair): M { for (element in this) { @@ -7763,6 +7857,8 @@ public inline fun > ByteArray.associateTo(desti * provided by [transform] function applied to each element of the given array. * * If any of two pairs would have the same key the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesTo */ public inline fun > ShortArray.associateTo(destination: M, transform: (Short) -> Pair): M { for (element in this) { @@ -7776,6 +7872,8 @@ public inline fun > ShortArray.associateTo(dest * provided by [transform] function applied to each element of the given array. * * If any of two pairs would have the same key the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesTo */ public inline fun > IntArray.associateTo(destination: M, transform: (Int) -> Pair): M { for (element in this) { @@ -7789,6 +7887,8 @@ public inline fun > IntArray.associateTo(destin * provided by [transform] function applied to each element of the given array. * * If any of two pairs would have the same key the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesTo */ public inline fun > LongArray.associateTo(destination: M, transform: (Long) -> Pair): M { for (element in this) { @@ -7802,6 +7902,8 @@ public inline fun > LongArray.associateTo(desti * provided by [transform] function applied to each element of the given array. * * If any of two pairs would have the same key the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesTo */ public inline fun > FloatArray.associateTo(destination: M, transform: (Float) -> Pair): M { for (element in this) { @@ -7815,6 +7917,8 @@ public inline fun > FloatArray.associateTo(dest * provided by [transform] function applied to each element of the given array. * * If any of two pairs would have the same key the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesTo */ public inline fun > DoubleArray.associateTo(destination: M, transform: (Double) -> Pair): M { for (element in this) { @@ -7828,6 +7932,8 @@ public inline fun > DoubleArray.associateTo(des * provided by [transform] function applied to each element of the given array. * * If any of two pairs would have the same key the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesTo */ public inline fun > BooleanArray.associateTo(destination: M, transform: (Boolean) -> Pair): M { for (element in this) { @@ -7841,6 +7947,8 @@ public inline fun > BooleanArray.associateTo(de * provided by [transform] function applied to each element of the given array. * * If any of two pairs would have the same key the last one gets added to the map. + * + * @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesTo */ public inline fun > CharArray.associateTo(destination: M, transform: (Char) -> Pair): M { for (element in this) { diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index dcf7c03ae69..6176604fea3 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -1056,6 +1056,8 @@ public fun Collection.toShortArray(): ShortArray { * If any of two pairs would have the same key the last one gets added to the map. * * The returned map preserves the entry iteration order of the original collection. + * + * @sample samples.collections.Collections.Transformations.associate */ public inline fun Iterable.associate(transform: (T) -> Pair): Map { val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) @@ -1069,6 +1071,8 @@ public inline fun Iterable.associate(transform: (T) -> Pair): * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original collection. + * + * @sample samples.collections.Collections.Transformations.associateBy */ public inline fun Iterable.associateBy(keySelector: (T) -> K): Map { val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) @@ -1081,6 +1085,8 @@ public inline fun Iterable.associateBy(keySelector: (T) -> K): Map Iterable.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) @@ -1093,6 +1099,8 @@ public inline fun Iterable.associateBy(keySelector: (T) -> K, value * and value is the element itself. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Collections.Transformations.associateByTo */ public inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K): M { for (element in this) { @@ -1107,6 +1115,8 @@ public inline fun > Iterable.associateByTo(d * and value is provided by the [valueTransform] function applied to elements of the given collection. * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.collections.Collections.Transformations.associateByToWithValueTransform */ public inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { for (element in this) { @@ -1120,6 +1130,8 @@ public inline fun > Iterable.associateByT * provided by [transform] function applied to each element of the given collection. * * If any of two pairs would have the same key the last one gets added to the map. + * + * @sample samples.collections.Collections.Transformations.associateTo */ public inline fun > Iterable.associateTo(destination: M, transform: (T) -> Pair): M { for (element in this) { @@ -1149,6 +1161,8 @@ public inline fun Iterable.associateWith(valueSelector: (K) -> V): Map * where key is the element itself and value is provided by the [valueSelector] function applied to that key. * * If any two elements are equal, the last one overwrites the former value in the map. + * + * @sample samples.collections.Collections.Transformations.associateWithTo */ @SinceKotlin("1.3") public inline fun > Iterable.associateWithTo(destination: M, valueSelector: (K) -> V): M { diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index 32f7ee41ff8..955ec005263 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -580,6 +580,8 @@ public fun Sequence.sortedWith(comparator: Comparator): Sequence * The returned map preserves the entry iteration order of the original sequence. * * The operation is _terminal_. + * + * @sample samples.collections.Collections.Transformations.associate */ public inline fun Sequence.associate(transform: (T) -> Pair): Map { return associateTo(LinkedHashMap(), transform) @@ -594,6 +596,8 @@ public inline fun Sequence.associate(transform: (T) -> Pair): * The returned map preserves the entry iteration order of the original sequence. * * The operation is _terminal_. + * + * @sample samples.collections.Collections.Transformations.associateBy */ public inline fun Sequence.associateBy(keySelector: (T) -> K): Map { return associateByTo(LinkedHashMap(), keySelector) @@ -607,6 +611,8 @@ public inline fun Sequence.associateBy(keySelector: (T) -> K): Map Sequence.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { return associateByTo(LinkedHashMap(), keySelector, valueTransform) @@ -620,6 +626,8 @@ public inline fun Sequence.associateBy(keySelector: (T) -> K, value * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The operation is _terminal_. + * + * @sample samples.collections.Collections.Transformations.associateByTo */ public inline fun > Sequence.associateByTo(destination: M, keySelector: (T) -> K): M { for (element in this) { @@ -636,6 +644,8 @@ public inline fun > Sequence.associateByTo(d * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. * * The operation is _terminal_. + * + * @sample samples.collections.Collections.Transformations.associateByToWithValueTransform */ public inline fun > Sequence.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { for (element in this) { @@ -651,6 +661,8 @@ public inline fun > Sequence.associateByT * If any of two pairs would have the same key the last one gets added to the map. * * The operation is _terminal_. + * + * @sample samples.collections.Collections.Transformations.associateTo */ public inline fun > Sequence.associateTo(destination: M, transform: (T) -> Pair): M { for (element in this) { @@ -684,6 +696,8 @@ public inline fun Sequence.associateWith(valueSelector: (K) -> V): Map * If any two elements are equal, the last one overwrites the former value in the map. * * The operation is _terminal_. + * + * @sample samples.collections.Collections.Transformations.associateWithTo */ @SinceKotlin("1.3") public inline fun > Sequence.associateWithTo(destination: M, valueSelector: (K) -> V): M { diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index 370b89cd002..e9b4dd5e2ac 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -583,6 +583,8 @@ public inline fun String.reversed(): String { * If any of two pairs would have the same key the last one gets added to the map. * * The returned map preserves the entry iteration order of the original char sequence. + * + * @sample samples.text.Strings.associate */ public inline fun CharSequence.associate(transform: (Char) -> Pair): Map { val capacity = mapCapacity(length).coerceAtLeast(16) @@ -596,6 +598,8 @@ public inline fun CharSequence.associate(transform: (Char) -> Pair) * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. * * The returned map preserves the entry iteration order of the original char sequence. + * + * @sample samples.text.Strings.associateBy */ public inline fun CharSequence.associateBy(keySelector: (Char) -> K): Map { val capacity = mapCapacity(length).coerceAtLeast(16) @@ -608,6 +612,8 @@ public inline fun CharSequence.associateBy(keySelector: (Char) -> K): Map CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map { val capacity = mapCapacity(length).coerceAtLeast(16) @@ -620,6 +626,8 @@ public inline fun CharSequence.associateBy(keySelector: (Char) -> K, valu * and value is the character itself. * * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.text.Strings.associateByTo */ public inline fun > CharSequence.associateByTo(destination: M, keySelector: (Char) -> K): M { for (element in this) { @@ -634,6 +642,8 @@ public inline fun > CharSequence.associateByTo( * and value is provided by the [valueTransform] function applied to characters of the given char sequence. * * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. + * + * @sample samples.text.Strings.associateByToWithValueTransform */ public inline fun > CharSequence.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { for (element in this) { @@ -647,6 +657,8 @@ public inline fun > CharSequence.associateByTo( * provided by [transform] function applied to each character of the given char sequence. * * If any of two pairs would have the same key the last one gets added to the map. + * + * @sample samples.text.Strings.associateTo */ public inline fun > CharSequence.associateTo(destination: M, transform: (Char) -> Pair): M { for (element in this) { @@ -676,6 +688,8 @@ public inline fun CharSequence.associateWith(valueSelector: (Char) -> V): Ma * where key is the character itself and value is provided by the [valueSelector] function applied to that key. * * If any two characters are equal, the last one overwrites the former value in the map. + * + * @sample samples.text.Strings.associateWithTo */ @SinceKotlin("1.3") public inline fun > CharSequence.associateWithTo(destination: M, valueSelector: (Char) -> V): M { diff --git a/libraries/stdlib/samples/test/samples/collections/arrays.kt b/libraries/stdlib/samples/test/samples/collections/arrays.kt index b70cc0f4705..0b737d366c3 100644 --- a/libraries/stdlib/samples/test/samples/collections/arrays.kt +++ b/libraries/stdlib/samples/test/samples/collections/arrays.kt @@ -64,6 +64,72 @@ class Arrays { class Transformations { + @Sample + fun associateArrayOfPrimitives() { + val charCodes = intArrayOf(72, 69, 76, 76, 79) + + val byCharCode = charCodes.associate { it to it.toChar() } + + // 76=L only occurs once because only the last pair with the same key gets added + assertPrints(byCharCode, "{72=H, 69=E, 76=L, 79=O}") + } + + + @Sample + fun associateArrayOfPrimitivesBy() { + val charCodes = intArrayOf(72, 69, 76, 76, 79) + + val byChar = charCodes.associateBy { it.toChar() } + + // L=76 only occurs once because only the last pair with the same key gets added + assertPrints(byChar, "{H=72, E=69, L=76, O=79}") + } + + @Sample + fun associateArrayOfPrimitivesByWithValueTransform() { + val charCodes = intArrayOf(65, 65, 66, 67, 68, 69) + + val byUpperCase = charCodes.associateBy({ it.toChar() }, { (it + 32).toChar() }) + + // A=a only occurs once because only the last pair with the same key gets added + assertPrints(byUpperCase, "{A=a, B=b, C=c, D=d, E=e}") + } + + @Sample + fun associateArrayOfPrimitivesByTo() { + val charCodes = intArrayOf(72, 69, 76, 76, 79) + val byChar = mutableMapOf() + + assertTrue(byChar.isEmpty()) + charCodes.associateByTo(byChar) { it.toChar() } + + assertTrue(byChar.isNotEmpty()) + // L=76 only occurs once because only the last pair with the same key gets added + assertPrints(byChar, "{H=72, E=69, L=76, O=79}") + } + + @Sample + fun associateArrayOfPrimitivesByToWithValueTransform() { + val charCodes = intArrayOf(65, 65, 66, 67, 68, 69) + + val byUpperCase = mutableMapOf() + charCodes.associateByTo(byUpperCase, { it.toChar() }, { (it + 32).toChar() } ) + + // A=a only occurs once because only the last pair with the same key gets added + assertPrints(byUpperCase, "{A=a, B=b, C=c, D=d, E=e}") + } + + @Sample + fun associateArrayOfPrimitivesTo() { + val charCodes = intArrayOf(72, 69, 76, 76, 79) + + val byChar = mutableMapOf() + charCodes.associateTo(byChar) { it to it.toChar() } + + // 76=L only occurs once because only the last pair with the same key gets added + assertPrints(byChar, "{72=H, 69=E, 76=L, 79=O}") + } + @Sample fun flattenArray() { val deepArray = arrayOf( diff --git a/libraries/stdlib/samples/test/samples/collections/collections.kt b/libraries/stdlib/samples/test/samples/collections/collections.kt index 9f4370619ad..1ddcf7318f7 100644 --- a/libraries/stdlib/samples/test/samples/collections/collections.kt +++ b/libraries/stdlib/samples/test/samples/collections/collections.kt @@ -337,6 +337,92 @@ class Collections { class Transformations { + @Sample + fun associate() { + val names = listOf("Grace Hopper", "Jacob Bernoulli", "Johann Bernoulli") + + val byLastName = names.associate { it.split(" ").let { (firstName, lastName) -> lastName to firstName } } + + // Jacob Bernoulli does not occur in the map because only the last pair with the same key gets added + assertPrints(byLastName, "{Hopper=Grace, Bernoulli=Johann}") + } + + @Sample + fun associateBy() { + data class Person(val firstName: String, val lastName: String) { + override fun toString(): String = "$firstName $lastName" + } + + val scientists = listOf(Person("Grace", "Hopper"), Person("Jacob", "Bernoulli"), Person("Johann", "Bernoulli")) + + val byLastName = scientists.associateBy { it.lastName } + + // Jacob Bernoulli does not occur in the map because only the last pair with the same key gets added + assertPrints(byLastName, "{Hopper=Grace Hopper, Bernoulli=Johann Bernoulli}") + } + + @Sample + fun associateByWithValueTransform() { + data class Person(val firstName: String, val lastName: String) + + val scientists = listOf(Person("Grace", "Hopper"), Person("Jacob", "Bernoulli"), Person("Johann", "Bernoulli")) + + val byLastName = scientists.associateBy({ it.lastName }, { it.firstName }) + + // Jacob Bernoulli does not occur in the map because only the last pair with the same key gets added + assertPrints(byLastName, "{Hopper=Grace, Bernoulli=Johann}") + } + + @Sample + fun associateByTo() { + data class Person(val firstName: String, val lastName: String) { + override fun toString(): String = "$firstName $lastName" + } + + val scientists = listOf(Person("Grace", "Hopper"), Person("Jacob", "Bernoulli"), Person("Johann", "Bernoulli")) + + val byLastName = mutableMapOf() + assertTrue(byLastName.isEmpty()) + + scientists.associateByTo(byLastName) { it.lastName } + + assertTrue(byLastName.isNotEmpty()) + // Jacob Bernoulli does not occur in the map because only the last pair with the same key gets added + assertPrints(byLastName, "{Hopper=Grace Hopper, Bernoulli=Johann Bernoulli}") + } + + @Sample + fun associateByToWithValueTransform() { + data class Person(val firstName: String, val lastName: String) + + val scientists = listOf(Person("Grace", "Hopper"), Person("Jacob", "Bernoulli"), Person("Johann", "Bernoulli")) + + val byLastName = mutableMapOf() + assertTrue(byLastName.isEmpty()) + + scientists.associateByTo(byLastName, { it.lastName }, { it.firstName} ) + + assertTrue(byLastName.isNotEmpty()) + // Jacob Bernoulli does not occur in the map because only the last pair with the same key gets added + assertPrints(byLastName, "{Hopper=Grace, Bernoulli=Johann}") + } + + @Sample + fun associateTo() { + data class Person(val firstName: String, val lastName: String) + + val scientists = listOf(Person("Grace", "Hopper"), Person("Jacob", "Bernoulli"), Person("Johann", "Bernoulli")) + + val byLastName = mutableMapOf() + assertTrue(byLastName.isEmpty()) + + scientists.associateTo(byLastName) { it.lastName to it.firstName } + + assertTrue(byLastName.isNotEmpty()) + // Jacob Bernoulli does not occur in the map because only the last pair with the same key gets added + assertPrints(byLastName, "{Hopper=Grace, Bernoulli=Johann}") + } + @Sample fun associateWith() { val words = listOf("a", "abc", "ab", "def", "abcd") @@ -345,6 +431,23 @@ class Collections { assertPrints(withLength.values, "[1, 3, 2, 3, 4]") } + @Sample + fun associateWithTo() { + data class Person(val firstName: String, val lastName: String) { + override fun toString(): String = "$firstName $lastName" + } + + val scientists = listOf(Person("Grace", "Hopper"), Person("Jacob", "Bernoulli"), Person("Jacob", "Bernoulli")) + val withLengthOfNames = mutableMapOf() + assertTrue(withLengthOfNames.isEmpty()) + + scientists.associateWithTo(withLengthOfNames) { it.firstName.length + it.lastName.length } + + assertTrue(withLengthOfNames.isNotEmpty()) + // Jacob Bernoulli only occurs once in the map because only the last pair with the same key gets added + assertPrints(withLengthOfNames, "{Grace Hopper=11, Jacob Bernoulli=14}") + } + @Sample fun groupBy() { val words = listOf("a", "abc", "ab", "def", "abcd") diff --git a/libraries/stdlib/samples/test/samples/text/strings.kt b/libraries/stdlib/samples/test/samples/text/strings.kt index 05e9579d950..5fc114cb564 100644 --- a/libraries/stdlib/samples/test/samples/text/strings.kt +++ b/libraries/stdlib/samples/test/samples/text/strings.kt @@ -85,6 +85,63 @@ class Strings { assertPrints(result, "[az, by, cx]") } + @Sample + fun associate() { + val string = "bonne journée" + // associate each character with its code + val result = string.associate { char -> char to char.toInt() } + // notice each letter occurs only once + assertPrints(result, "{b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}") + } + + @Sample + fun associateBy() { + val string = "bonne journée" + // associate each character by its code + val result = string.associateBy { char -> char.toInt() } + // notice each char code occurs only once + assertPrints(result, "{98=b, 111=o, 110=n, 101=e, 32= , 106=j, 117=u, 114=r, 233=é}") + } + + @Sample + fun associateByWithValueTransform() { + val string = "bonne journée" + // associate each character by the code of its upper case equivalent and transform the character to upper case + val result = string.associateBy({ char -> char.toUpperCase().toInt() }, { char -> char.toUpperCase() }) + // notice each char code occurs only once + assertPrints(result, "{66=B, 79=O, 78=N, 69=E, 32= , 74=J, 85=U, 82=R, 201=É}") + } + + @Sample + fun associateByTo() { + val string = "bonne journée" + // associate each character by its code + val result = mutableMapOf() + string.associateByTo(result) { char -> char.toInt() } + // notice each char code occurs only once + assertPrints(result, "{98=b, 111=o, 110=n, 101=e, 32= , 106=j, 117=u, 114=r, 233=é}") + } + + @Sample + fun associateByToWithValueTransform() { + val string = "bonne journée" + // associate each character by the code of its upper case equivalent and transform the character to upper case + val result = mutableMapOf() + string.associateByTo(result, { char -> char.toUpperCase().toInt() }, { char -> char.toUpperCase() }) + // notice each char code occurs only once + assertPrints(result, "{66=B, 79=O, 78=N, 69=E, 32= , 74=J, 85=U, 82=R, 201=É}") + } + + @Sample + fun associateTo() { + val string = "bonne journée" + // associate each character with its code + val result = mutableMapOf() + string.associateTo(result) { char -> char to char.toInt() } + // notice each letter occurs only once + assertPrints(result, "{b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}") + } + @Sample fun associateWith() { val string = "bonne journée" @@ -94,6 +151,16 @@ class Strings { assertPrints(result, "{b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}") } + @Sample + fun associateWithTo() { + val string = "bonne journée" + // associate each character with its code + val result = mutableMapOf() + string.associateWithTo(result) { char -> char.toInt() } + // notice each letter occurs only once + assertPrints(result, "{b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}") + } + @Sample fun stringToByteArray() { val charset = Charsets.UTF_8 diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index fabfad70a3d..03582e9f46d 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -205,6 +205,11 @@ object Snapshots : TemplateGroupBase() { The returned map preserves the entry iteration order of the original ${f.collection}. """ } + sample(when (family) { + CharSequences -> "samples.text.Strings.associate" + ArraysOfObjects, ArraysOfPrimitives -> "samples.collections.Arrays.Transformations.associateArrayOfPrimitives" + else -> "samples.collections.Collections.Transformations.associate" + }) body { """ val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) @@ -247,6 +252,11 @@ object Snapshots : TemplateGroupBase() { If any of two pairs would have the same key the last one gets added to the map. """ } + sample(when (family) { + CharSequences -> "samples.text.Strings.associateTo" + ArraysOfObjects, ArraysOfPrimitives -> "samples.collections.Arrays.Transformations.associateArrayOfPrimitivesTo" + else -> "samples.collections.Collections.Transformations.associateTo" + }) body { """ for (element in this) { @@ -273,6 +283,11 @@ object Snapshots : TemplateGroupBase() { The returned map preserves the entry iteration order of the original ${f.collection}. """ } + sample(when (family) { + CharSequences -> "samples.text.Strings.associateBy" + ArraysOfObjects, ArraysOfPrimitives -> "samples.collections.Arrays.Transformations.associateArrayOfPrimitivesBy" + else -> "samples.collections.Collections.Transformations.associateBy" + }) returns("Map") // Collection size helper methods are private, so we fall back to the calculation from HashSet's Collection @@ -319,6 +334,11 @@ object Snapshots : TemplateGroupBase() { If any two ${f.element.pluralize()} would have the same key returned by [keySelector] the last one gets added to the map. """ } + sample(when (family) { + CharSequences -> "samples.text.Strings.associateByTo" + ArraysOfObjects, ArraysOfPrimitives -> "samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByTo" + else -> "samples.collections.Collections.Transformations.associateByTo" + }) body { """ for (element in this) { @@ -345,6 +365,11 @@ object Snapshots : TemplateGroupBase() { The returned map preserves the entry iteration order of the original ${f.collection}. """ } + sample(when (family) { + CharSequences -> "samples.text.Strings.associateByWithValueTransform" + ArraysOfObjects, ArraysOfPrimitives -> "samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform" + else -> "samples.collections.Collections.Transformations.associateByWithValueTransform" + }) returns("Map") /** @@ -396,6 +421,11 @@ object Snapshots : TemplateGroupBase() { If any two ${f.element.pluralize()} would have the same key returned by [keySelector] the last one gets added to the map. """ } + sample(when (family) { + CharSequences -> "samples.text.Strings.associateByToWithValueTransform" + ArraysOfObjects, ArraysOfPrimitives -> "samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByToWithValueTransform" + else -> "samples.collections.Collections.Transformations.associateByToWithValueTransform" + }) body { """ for (element in this) { @@ -458,6 +488,10 @@ object Snapshots : TemplateGroupBase() { If any two ${f.element.pluralize()} are equal, the last one overwrites the former value in the map. """ } + sample(when (family) { + CharSequences -> "samples.text.Strings.associateWithTo" + else -> "samples.collections.Collections.Transformations.associateWithTo" + }) body { """ for (element in this) {