Permalink
Browse files

#KT-1775 Fixed

  • Loading branch information...
1 parent bc00216 commit 33e0e2d99172305314afe566e3fc8a43de93032b @jstrachan jstrachan committed Apr 12, 2012
@@ -10,15 +10,18 @@ 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
*/
public inline fun <T, R> java.util.Collection<T>.map(transform : (T) -> R) : java.util.List<R> {
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 {
for (item in this)
result.add(transform(item))
@@ -1,10 +1,11 @@
package kotlin
import java.util.Map as JMap
-import java.util.HashMap
-import java.util.Collections
-
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
@@ -71,4 +72,44 @@ public inline fun <K,V> java.util.Map<K,V>.getOrPut(key: K, defaultValue: ()-> V
public inline fun <K,V> java.util.Map<K,V>.iterator(): java.util.Iterator<java.util.Map.Entry<K,V>> {
val entrySet = this.entrySet()!!
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
+}
@@ -79,4 +79,36 @@ class MapTest {
assertEquals(6, list.size())
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())
+ }
}

0 comments on commit 33e0e2d

Please sign in to comment.