Introduce associateWith and associateWithTo functions
#KT-13814
This commit is contained in:
+6
@@ -2084,6 +2084,8 @@ public final class kotlin/collections/CollectionsKt {
|
||||
public static final fun associateByTo (Ljava/lang/Iterable;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateByTo (Ljava/lang/Iterable;Ljava/util/Map;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateTo (Ljava/lang/Iterable;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateWith (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateWithTo (Ljava/lang/Iterable;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun averageOfByte (Ljava/lang/Iterable;)D
|
||||
public static final fun averageOfDouble (Ljava/lang/Iterable;)D
|
||||
public static final fun averageOfFloat (Ljava/lang/Iterable;)D
|
||||
@@ -4356,6 +4358,8 @@ public final class kotlin/sequences/SequencesKt {
|
||||
public static final fun associateByTo (Lkotlin/sequences/Sequence;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateByTo (Lkotlin/sequences/Sequence;Ljava/util/Map;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateTo (Lkotlin/sequences/Sequence;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateWith (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateWithTo (Lkotlin/sequences/Sequence;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun averageOfByte (Lkotlin/sequences/Sequence;)D
|
||||
public static final fun averageOfDouble (Lkotlin/sequences/Sequence;)D
|
||||
public static final fun averageOfFloat (Lkotlin/sequences/Sequence;)D
|
||||
@@ -4693,6 +4697,8 @@ public final class kotlin/text/StringsKt {
|
||||
public static final fun associateByTo (Ljava/lang/CharSequence;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateByTo (Ljava/lang/CharSequence;Ljava/util/Map;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateTo (Ljava/lang/CharSequence;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateWith (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateWithTo (Ljava/lang/CharSequence;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun capitalize (Ljava/lang/String;)Ljava/lang/String;
|
||||
public static final fun chunked (Ljava/lang/CharSequence;I)Ljava/util/List;
|
||||
public static final fun chunked (Ljava/lang/CharSequence;ILkotlin/jvm/functions/Function1;)Ljava/util/List;
|
||||
|
||||
@@ -405,4 +405,66 @@ object Snapshots : TemplateGroupBase() {
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_associateWith = fn("associateWith(valueSelector: (K) -> V)") {
|
||||
include(Iterables, Sequences, CharSequences)
|
||||
} builder {
|
||||
inline()
|
||||
since("1.3")
|
||||
typeParam("K", primary = true)
|
||||
typeParam("V")
|
||||
returns("Map<K, V>")
|
||||
doc {
|
||||
"""
|
||||
Returns a [Map] where keys are ${f.element.pluralize()} from the given ${f.collection} and values are
|
||||
produced by the [valueSelector] function applied to each ${f.element}.
|
||||
|
||||
If any two ${f.element.pluralize()} are equal, the last one gets added to the map.
|
||||
|
||||
The returned map preserves the entry iteration order of the original ${f.collection}.
|
||||
"""
|
||||
}
|
||||
sample(when (family) {
|
||||
CharSequences -> "samples.text.Strings.associateWith"
|
||||
else -> "samples.collections.Collections.Transformations.associateWith"
|
||||
})
|
||||
body {
|
||||
val resultMap = when (family) {
|
||||
Iterables -> "LinkedHashMap<K, V>(mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16))"
|
||||
CharSequences -> "LinkedHashMap<K, V>(mapCapacity(length).coerceAtLeast(16))"
|
||||
else -> "LinkedHashMap<K, V>()"
|
||||
}
|
||||
"""
|
||||
val result = $resultMap
|
||||
return associateWithTo(result, valueSelector)
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_associateWithTo = fn("associateWithTo(destination: M, valueSelector: (K) -> V)") {
|
||||
include(Iterables, Sequences, CharSequences)
|
||||
} builder {
|
||||
inline()
|
||||
since("1.3")
|
||||
typeParam("K", primary = true)
|
||||
typeParam("V")
|
||||
typeParam("M : MutableMap<in K, in V>")
|
||||
returns("M")
|
||||
doc {
|
||||
"""
|
||||
Populates and returns the [destination] mutable map with key-value pairs for each ${f.element} of the given ${f.collection},
|
||||
where key is the ${f.element} itself and value is provided by the [valueSelector] function applied to that key.
|
||||
|
||||
If any two ${f.element.pluralize()} are equal, the last one overwrites the former value in the map.
|
||||
"""
|
||||
}
|
||||
body {
|
||||
"""
|
||||
for (element in this) {
|
||||
destination.put(element, valueSelector(element))
|
||||
}
|
||||
return destination
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user