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
This commit is contained in:
Dat Trieu
2019-11-29 14:54:02 +01:00
committed by ilya-g
parent d2e5432b2d
commit f334ad2ffc
8 changed files with 420 additions and 0 deletions
@@ -7145,6 +7145,8 @@ public expect fun CharArray.toTypedArray(): Array<Char>
* 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 <T, K, V> Array<out T>.associate(transform: (T) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7158,6 +7160,8 @@ public inline fun <T, K, V> Array<out T>.associate(transform: (T) -> Pair<K, V>)
* 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 <K, V> ByteArray.associate(transform: (Byte) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7171,6 +7175,8 @@ public inline fun <K, V> ByteArray.associate(transform: (Byte) -> Pair<K, V>): 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 <K, V> ShortArray.associate(transform: (Short) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7184,6 +7190,8 @@ public inline fun <K, V> ShortArray.associate(transform: (Short) -> Pair<K, V>):
* 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 <K, V> IntArray.associate(transform: (Int) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7197,6 +7205,8 @@ public inline fun <K, V> IntArray.associate(transform: (Int) -> Pair<K, V>): 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 <K, V> LongArray.associate(transform: (Long) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7210,6 +7220,8 @@ public inline fun <K, V> LongArray.associate(transform: (Long) -> Pair<K, V>): 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 <K, V> FloatArray.associate(transform: (Float) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7223,6 +7235,8 @@ public inline fun <K, V> FloatArray.associate(transform: (Float) -> Pair<K, V>):
* 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 <K, V> DoubleArray.associate(transform: (Double) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7236,6 +7250,8 @@ public inline fun <K, V> DoubleArray.associate(transform: (Double) -> Pair<K, V>
* 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 <K, V> BooleanArray.associate(transform: (Boolean) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7249,6 +7265,8 @@ public inline fun <K, V> BooleanArray.associate(transform: (Boolean) -> Pair<K,
* 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 <K, V> CharArray.associate(transform: (Char) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7262,6 +7280,8 @@ public inline fun <K, V> CharArray.associate(transform: (Char) -> Pair<K, V>): 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 <T, K> Array<out T>.associateBy(keySelector: (T) -> K): Map<K, T> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7275,6 +7295,8 @@ public inline fun <T, K> Array<out T>.associateBy(keySelector: (T) -> K): Map<K,
* 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 <K> ByteArray.associateBy(keySelector: (Byte) -> K): Map<K, Byte> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7288,6 +7310,8 @@ public inline fun <K> ByteArray.associateBy(keySelector: (Byte) -> K): Map<K, By
* 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 <K> ShortArray.associateBy(keySelector: (Short) -> K): Map<K, Short> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7301,6 +7325,8 @@ public inline fun <K> ShortArray.associateBy(keySelector: (Short) -> K): Map<K,
* 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 <K> IntArray.associateBy(keySelector: (Int) -> K): Map<K, Int> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7314,6 +7340,8 @@ public inline fun <K> IntArray.associateBy(keySelector: (Int) -> K): Map<K, Int>
* 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 <K> LongArray.associateBy(keySelector: (Long) -> K): Map<K, Long> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7327,6 +7355,8 @@ public inline fun <K> LongArray.associateBy(keySelector: (Long) -> K): Map<K, Lo
* 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 <K> FloatArray.associateBy(keySelector: (Float) -> K): Map<K, Float> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7340,6 +7370,8 @@ public inline fun <K> FloatArray.associateBy(keySelector: (Float) -> K): Map<K,
* 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 <K> DoubleArray.associateBy(keySelector: (Double) -> K): Map<K, Double> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7353,6 +7385,8 @@ public inline fun <K> DoubleArray.associateBy(keySelector: (Double) -> K): Map<K
* 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 <K> BooleanArray.associateBy(keySelector: (Boolean) -> K): Map<K, Boolean> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7366,6 +7400,8 @@ public inline fun <K> 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 <K> CharArray.associateBy(keySelector: (Char) -> K): Map<K, Char> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7378,6 +7414,8 @@ public inline fun <K> CharArray.associateBy(keySelector: (Char) -> K): Map<K, Ch
* 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 <T, K, V> Array<out T>.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7390,6 +7428,8 @@ public inline fun <T, K, V> Array<out T>.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 <K, V> ByteArray.associateBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7402,6 +7442,8 @@ public inline fun <K, V> 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 <K, V> ShortArray.associateBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7414,6 +7456,8 @@ public inline fun <K, V> 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 <K, V> IntArray.associateBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7426,6 +7470,8 @@ public inline fun <K, V> 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 <K, V> LongArray.associateBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7438,6 +7484,8 @@ public inline fun <K, V> 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 <K, V> FloatArray.associateBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7450,6 +7498,8 @@ public inline fun <K, V> 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 <K, V> DoubleArray.associateBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7462,6 +7512,8 @@ public inline fun <K, V> 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 <K, V> BooleanArray.associateBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7474,6 +7526,8 @@ public inline fun <K, V> 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 <K, V> CharArray.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
@@ -7486,6 +7540,8 @@ public inline fun <K, V> 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 <T, K, M : MutableMap<in K, in T>> Array<out T>.associateByTo(destination: M, keySelector: (T) -> K): M {
for (element in this) {
@@ -7500,6 +7556,8 @@ public inline fun <T, K, M : MutableMap<in K, in T>> Array<out T>.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 <K, M : MutableMap<in K, in Byte>> ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K): M {
for (element in this) {
@@ -7514,6 +7572,8 @@ public inline fun <K, M : MutableMap<in K, in Byte>> 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 <K, M : MutableMap<in K, in Short>> ShortArray.associateByTo(destination: M, keySelector: (Short) -> K): M {
for (element in this) {
@@ -7528,6 +7588,8 @@ public inline fun <K, M : MutableMap<in K, in Short>> 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 <K, M : MutableMap<in K, in Int>> IntArray.associateByTo(destination: M, keySelector: (Int) -> K): M {
for (element in this) {
@@ -7542,6 +7604,8 @@ public inline fun <K, M : MutableMap<in K, in Int>> 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 <K, M : MutableMap<in K, in Long>> LongArray.associateByTo(destination: M, keySelector: (Long) -> K): M {
for (element in this) {
@@ -7556,6 +7620,8 @@ public inline fun <K, M : MutableMap<in K, in Long>> 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 <K, M : MutableMap<in K, in Float>> FloatArray.associateByTo(destination: M, keySelector: (Float) -> K): M {
for (element in this) {
@@ -7570,6 +7636,8 @@ public inline fun <K, M : MutableMap<in K, in Float>> 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 <K, M : MutableMap<in K, in Double>> DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K): M {
for (element in this) {
@@ -7584,6 +7652,8 @@ public inline fun <K, M : MutableMap<in K, in Double>> 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 <K, M : MutableMap<in K, in Boolean>> BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K): M {
for (element in this) {
@@ -7598,6 +7668,8 @@ public inline fun <K, M : MutableMap<in K, in Boolean>> 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 <K, M : MutableMap<in K, in Char>> CharArray.associateByTo(destination: M, keySelector: (Char) -> K): M {
for (element in this) {
@@ -7612,6 +7684,8 @@ public inline fun <K, M : MutableMap<in K, in Char>> 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 <T, K, V, M : MutableMap<in K, in V>> Array<out T>.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M {
for (element in this) {
@@ -7626,6 +7700,8 @@ public inline fun <T, K, V, M : MutableMap<in K, in V>> Array<out T>.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 <K, V, M : MutableMap<in K, in V>> ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M {
for (element in this) {
@@ -7640,6 +7716,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> ShortArray.associateByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M {
for (element in this) {
@@ -7654,6 +7732,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> IntArray.associateByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M {
for (element in this) {
@@ -7668,6 +7748,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> LongArray.associateByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M {
for (element in this) {
@@ -7682,6 +7764,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> FloatArray.associateByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M {
for (element in this) {
@@ -7696,6 +7780,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M {
for (element in this) {
@@ -7710,6 +7796,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M {
for (element in this) {
@@ -7724,6 +7812,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> CharArray.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M {
for (element in this) {
@@ -7737,6 +7827,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <T, K, V, M : MutableMap<in K, in V>> Array<out T>.associateTo(destination: M, transform: (T) -> Pair<K, V>): M {
for (element in this) {
@@ -7750,6 +7842,8 @@ public inline fun <T, K, V, M : MutableMap<in K, in V>> Array<out T>.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 <K, V, M : MutableMap<in K, in V>> ByteArray.associateTo(destination: M, transform: (Byte) -> Pair<K, V>): M {
for (element in this) {
@@ -7763,6 +7857,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> ShortArray.associateTo(destination: M, transform: (Short) -> Pair<K, V>): M {
for (element in this) {
@@ -7776,6 +7872,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> IntArray.associateTo(destination: M, transform: (Int) -> Pair<K, V>): M {
for (element in this) {
@@ -7789,6 +7887,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> LongArray.associateTo(destination: M, transform: (Long) -> Pair<K, V>): M {
for (element in this) {
@@ -7802,6 +7902,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> FloatArray.associateTo(destination: M, transform: (Float) -> Pair<K, V>): M {
for (element in this) {
@@ -7815,6 +7917,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> DoubleArray.associateTo(destination: M, transform: (Double) -> Pair<K, V>): M {
for (element in this) {
@@ -7828,6 +7932,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> BooleanArray.associateTo(destination: M, transform: (Boolean) -> Pair<K, V>): M {
for (element in this) {
@@ -7841,6 +7947,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> CharArray.associateTo(destination: M, transform: (Char) -> Pair<K, V>): M {
for (element in this) {
@@ -1056,6 +1056,8 @@ public fun Collection<Short>.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 <T, K, V> Iterable<T>.associate(transform: (T) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
@@ -1069,6 +1071,8 @@ public inline fun <T, K, V> Iterable<T>.associate(transform: (T) -> Pair<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 collection.
*
* @sample samples.collections.Collections.Transformations.associateBy
*/
public inline fun <T, K> Iterable<T>.associateBy(keySelector: (T) -> K): Map<K, T> {
val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
@@ -1081,6 +1085,8 @@ public inline fun <T, K> Iterable<T>.associateBy(keySelector: (T) -> K): Map<K,
* 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.associateByWithValueTransform
*/
public inline fun <T, K, V> Iterable<T>.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, V> {
val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
@@ -1093,6 +1099,8 @@ public inline fun <T, K, V> Iterable<T>.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 <T, K, M : MutableMap<in K, in T>> Iterable<T>.associateByTo(destination: M, keySelector: (T) -> K): M {
for (element in this) {
@@ -1107,6 +1115,8 @@ public inline fun <T, K, M : MutableMap<in K, in T>> Iterable<T>.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 <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M {
for (element in this) {
@@ -1120,6 +1130,8 @@ public inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.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 <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateTo(destination: M, transform: (T) -> Pair<K, V>): M {
for (element in this) {
@@ -1149,6 +1161,8 @@ public inline fun <K, V> Iterable<K>.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 <K, V, M : MutableMap<in K, in V>> Iterable<K>.associateWithTo(destination: M, valueSelector: (K) -> V): M {
@@ -580,6 +580,8 @@ public fun <T> Sequence<T>.sortedWith(comparator: Comparator<in T>): Sequence<T>
* 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 <T, K, V> Sequence<T>.associate(transform: (T) -> Pair<K, V>): Map<K, V> {
return associateTo(LinkedHashMap<K, V>(), transform)
@@ -594,6 +596,8 @@ public inline fun <T, K, V> Sequence<T>.associate(transform: (T) -> Pair<K, V>):
* 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 <T, K> Sequence<T>.associateBy(keySelector: (T) -> K): Map<K, T> {
return associateByTo(LinkedHashMap<K, T>(), keySelector)
@@ -607,6 +611,8 @@ public inline fun <T, K> Sequence<T>.associateBy(keySelector: (T) -> K): Map<K,
* The returned map preserves the entry iteration order of the original sequence.
*
* The operation is _terminal_.
*
* @sample samples.collections.Collections.Transformations.associateByWithValueTransform
*/
public inline fun <T, K, V> Sequence<T>.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, V> {
return associateByTo(LinkedHashMap<K, V>(), keySelector, valueTransform)
@@ -620,6 +626,8 @@ public inline fun <T, K, V> Sequence<T>.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 <T, K, M : MutableMap<in K, in T>> Sequence<T>.associateByTo(destination: M, keySelector: (T) -> K): M {
for (element in this) {
@@ -636,6 +644,8 @@ public inline fun <T, K, M : MutableMap<in K, in T>> Sequence<T>.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 <T, K, V, M : MutableMap<in K, in V>> Sequence<T>.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M {
for (element in this) {
@@ -651,6 +661,8 @@ public inline fun <T, K, V, M : MutableMap<in K, in V>> Sequence<T>.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 <T, K, V, M : MutableMap<in K, in V>> Sequence<T>.associateTo(destination: M, transform: (T) -> Pair<K, V>): M {
for (element in this) {
@@ -684,6 +696,8 @@ public inline fun <K, V> Sequence<K>.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 <K, V, M : MutableMap<in K, in V>> Sequence<K>.associateWithTo(destination: M, valueSelector: (K) -> V): M {
@@ -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 <K, V> CharSequence.associate(transform: (Char) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(length).coerceAtLeast(16)
@@ -596,6 +598,8 @@ public inline fun <K, V> CharSequence.associate(transform: (Char) -> Pair<K, V>)
* 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 <K> CharSequence.associateBy(keySelector: (Char) -> K): Map<K, Char> {
val capacity = mapCapacity(length).coerceAtLeast(16)
@@ -608,6 +612,8 @@ public inline fun <K> CharSequence.associateBy(keySelector: (Char) -> K): Map<K,
* 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.associateByWithValueTransform
*/
public inline fun <K, V> CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, V> {
val capacity = mapCapacity(length).coerceAtLeast(16)
@@ -620,6 +626,8 @@ public inline fun <K, V> 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 <K, M : MutableMap<in K, in Char>> CharSequence.associateByTo(destination: M, keySelector: (Char) -> K): M {
for (element in this) {
@@ -634,6 +642,8 @@ public inline fun <K, M : MutableMap<in K, in Char>> 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 <K, V, M : MutableMap<in K, in V>> CharSequence.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M {
for (element in this) {
@@ -647,6 +657,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> 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 <K, V, M : MutableMap<in K, in V>> CharSequence.associateTo(destination: M, transform: (Char) -> Pair<K, V>): M {
for (element in this) {
@@ -676,6 +688,8 @@ public inline fun <V> 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 <V, M : MutableMap<in Char, in V>> CharSequence.associateWithTo(destination: M, valueSelector: (Char) -> V): M {
@@ -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<Char, Int>()
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<Char, Char>()
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<Int, Char>()
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(
@@ -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<String, Person>()
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<String, String>()
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<String, String>()
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<Person, Int>()
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")
@@ -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<Int, Char>()
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<Int, Char>()
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<Char, Int>()
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<Char, Int>()
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
@@ -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<K, T>")
// 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<K, V>")
/**
@@ -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) {