Introduce associateWith and associateWithTo functions

#KT-13814
This commit is contained in:
Ilya Gorbunov
2018-08-20 22:01:12 +03:00
parent 751e844258
commit f367322084
10 changed files with 211 additions and 0 deletions
@@ -1059,6 +1059,36 @@ public inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateTo(
return destination
}
/**
* Returns a [Map] where keys are elements from the given collection and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, 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.associateWith
*/
@SinceKotlin("1.3")
public inline fun <K, V> Iterable<K>.associateWith(valueSelector: (K) -> V): Map<K, V> {
val result = LinkedHashMap<K, V>(mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16))
return associateWithTo(result, valueSelector)
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given collection,
* 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.
*/
@SinceKotlin("1.3")
public inline fun <K, V, M : MutableMap<in K, in V>> Iterable<K>.associateWithTo(destination: M, valueSelector: (K) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Appends all elements to the given [destination] collection.
*/
@@ -642,6 +642,40 @@ public inline fun <T, K, V, M : MutableMap<in K, in V>> Sequence<T>.associateTo(
return destination
}
/**
* Returns a [Map] where keys are elements from the given sequence and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original sequence.
*
* The operation is _terminal_.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.3")
public inline fun <K, V> Sequence<K>.associateWith(valueSelector: (K) -> V): Map<K, V> {
val result = LinkedHashMap<K, V>()
return associateWithTo(result, valueSelector)
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given sequence,
* 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.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.3")
public inline fun <K, V, M : MutableMap<in K, in V>> Sequence<K>.associateWithTo(destination: M, valueSelector: (K) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Appends all elements to the given [destination] collection.
*
@@ -615,6 +615,36 @@ public inline fun <K, V, M : MutableMap<in K, in V>> CharSequence.associateTo(de
return destination
}
/**
* Returns a [Map] where keys are characters from the given char sequence and values are
* produced by the [valueSelector] function applied to each character.
*
* If any two characters are equal, 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.associateWith
*/
@SinceKotlin("1.3")
public inline fun <V> CharSequence.associateWith(valueSelector: (Char) -> V): Map<Char, V> {
val result = LinkedHashMap<Char, V>(mapCapacity(length).coerceAtLeast(16))
return associateWithTo(result, valueSelector)
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each character of the given char sequence,
* 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.
*/
@SinceKotlin("1.3")
public inline fun <V, M : MutableMap<in Char, in V>> CharSequence.associateWithTo(destination: M, valueSelector: (Char) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Appends all characters to the given [destination] collection.
*/