allow mapValues() to be used on a Map not just a MutableMap

This commit is contained in:
James Strachan
2012-09-26 09:04:08 +01:00
parent f519913d8a
commit b5145f1a87
2 changed files with 4 additions and 4 deletions
+3 -3
View File
@@ -93,7 +93,7 @@ public inline fun <K,V,R, C: MutableCollection<in R>> Map<K,V>.mapTo(result: C,
/**
* Populates the given *result* [[Map]] with the value returned by applying the *transform* function on each [[Map.Entry]] in this [[Map]]
*/
public inline fun <K,V,R,C: MutableMap<K,R>> MutableMap<K,V>.mapValuesTo(result: C, transform : (Map.Entry<K,V>) -> R) : C {
public inline fun <K,V,R,C: MutableMap<K,R>> Map<K,V>.mapValuesTo(result: C, transform : (Map.Entry<K,V>) -> R) : C {
for (e in this) {
val newValue = transform(e)
result.put(e.key, newValue)
@@ -102,7 +102,7 @@ public inline fun <K,V,R,C: MutableMap<K,R>> MutableMap<K,V>.mapValuesTo(result:
}
/**
* Puts all the entries into the map with the first value in the tuple being the key and the second the value
* Puts all the entries into this [[MutableMap]] with the first value in the pair being the key and the second the value
*/
public inline fun <K,V> MutableMap<K,V>.putAll(vararg values: Pair<K, V>): Unit {
for (v in values) {
@@ -111,7 +111,7 @@ public inline fun <K,V> MutableMap<K,V>.putAll(vararg values: Pair<K, V>): Unit
}
/**
* Copies the entries in this [[Map]] to the given *map*
* Copies the entries in this [[Map]] to the given mutable *map*
*/
public inline fun <K,V> Map<K,V>.toMap(map: MutableMap<K,V>): Map<K,V> {
map.putAll(this)
+1 -1
View File
@@ -57,7 +57,7 @@ public inline fun <K,V,R> Map<K,V>.map(transform: (Map.Entry<K,V>) -> R) : List<
*
* @includeFunctionBody ../../test/MapTest.kt mapValues
*/
public inline fun <K,V,R> MutableMap<K,V>.mapValues(transform : (Map.Entry<K,V>) -> R): Map<K,R> {
public inline fun <K,V,R> Map<K,V>.mapValues(transform : (Map.Entry<K,V>) -> R): Map<K,R> {
return mapValuesTo(java.util.HashMap<K,R>(this.size), transform)
}