#KT-1775 Fixed

This commit is contained in:
James Strachan
2012-04-12 18:13:43 +01:00
parent bc00216df9
commit 33e0e2d991
3 changed files with 82 additions and 6 deletions
@@ -10,7 +10,7 @@ import java.util.*
// //
/** /**
* Returns a new List containing the results of applying the given function to each element in this collection * Returns a new List containing the results of applying the given *transform* function to each element in this collection
* *
* @includeFunctionBody ../../test/CollectionTest.kt map * @includeFunctionBody ../../test/CollectionTest.kt map
*/ */
@@ -18,7 +18,10 @@ public inline fun <T, R> java.util.Collection<T>.map(transform : (T) -> R) : jav
return mapTo(java.util.ArrayList<R>(this.size), transform) return mapTo(java.util.ArrayList<R>(this.size), transform)
} }
/** Transforms each element of this collection with the given function then adds the results to the given collection */ /**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <T, R, C: Collection<in R>> java.util.Collection<T>.mapTo(result: C, transform : (T) -> R) : C { public inline fun <T, R, C: Collection<in R>> java.util.Collection<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this) for (item in this)
result.add(transform(item)) result.add(transform(item))
+44 -3
View File
@@ -1,10 +1,11 @@
package kotlin package kotlin
import java.util.Map as JMap import java.util.Map as JMap
import java.util.HashMap
import java.util.Collections
import java.util.Map.Entry as JEntry import java.util.Map.Entry as JEntry
import java.util.Collection
import java.util.Collections
import java.util.HashMap
import java.util.List
// Map APIs // Map APIs
@@ -72,3 +73,43 @@ public inline fun <K,V> java.util.Map<K,V>.iterator(): java.util.Iterator<java.u
val entrySet = this.entrySet()!! val entrySet = this.entrySet()!!
return entrySet.iterator()!! return entrySet.iterator()!!
} }
/**
* Returns a new List containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]]
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <K,V,R> java.util.Map<K,V>.map(transform: (java.util.Map.Entry<K,V>) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
/**
* Transforms each [[Map.Entry]] in this [[Map]] with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <K,V,R, C: Collection<in R>> java.util.Map<K,V>.mapTo(result: C, transform: (java.util.Map.Entry<K,V>) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns a new Map containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]]
*
* @includeFunctionBody ../../test/MapTest.kt map
*/
public inline fun <K,V,R> java.util.Map<K,V>.mapValues(transform : (java.util.Map.Entry<K,V>) -> R): java.util.Map<K,R> {
return mapValuesTo(java.util.HashMap<K,R>(this.size), transform)
}
/**
* 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: java.util.Map<K,R>> java.util.Map<K,V>.mapValuesTo(result: C, transform : (java.util.Map.Entry<K,V>) -> R) : C {
for (e in this) {
val newValue = transform(e)
result.put(e.key, newValue)
}
return result
}
+32
View File
@@ -79,4 +79,36 @@ class MapTest {
assertEquals(6, list.size()) assertEquals(6, list.size())
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(",")) assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
} }
/*
TODO compiler bug
we should be able to remove the explicit type <String,String,String> on the map function
http://youtrack.jetbrains.net/issue/KT-1145
*/
test fun map() {
val m1 = TreeMap<String, String>()
m1["beverage"] = "beer"
m1["location"] = "Mells"
val list = m1.map<String,String,String>{ it.value + " rocks" }
println("Got new list $list")
assertEquals(arrayList("beer rocks", "Mells rocks"), list)
}
/*
TODO compiler bug
we should be able to remove the explicit type <String,String,String> on the mapValues function
http://youtrack.jetbrains.net/issue/KT-1145
*/
test fun mapValues() {
val m1 = TreeMap<String, String>()
m1["beverage"] = "beer"
m1["location"] = "Mells"
val m2 = m1.mapValues<String,String,String>{ it.value + "2" }
println("Got new map $m2")
assertEquals(arrayList("beer2", "Mells2"), m2.values().toList())
}
} }