From c3f1e38c672fe658dbc842ac28db365f8f94168f Mon Sep 17 00:00:00 2001 From: James Strachan Date: Thu, 12 Apr 2012 12:35:13 +0100 Subject: [PATCH] avoid default arguments on groupBy() by having groupByTo() if the caller wishes to specify the Map to group into. Also tidied up the sample code in CollectionTest --- .../src/generated/ArraysFromJLangIterables.kt | 13 +++++-- .../JUtilIteratorsFromJLangIterables.kt | 13 +++++-- .../generated/StandardFromJLangIterables.kt | 13 +++++-- libraries/stdlib/src/kotlin/JLangIterables.kt | 11 ++++-- libraries/stdlib/test/CollectionTest.kt | 34 ++++--------------- .../jetbrains/kotlin/doc/model/KotlinModel.kt | 6 ++-- 6 files changed, 49 insertions(+), 41 deletions(-) diff --git a/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt index c4311339aee..c8d1fed1ab7 100644 --- a/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt @@ -141,11 +141,18 @@ public inline fun Array.fold(initial: T, operation: (T, T) -> T): T { public inline fun Array.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) /** - * Transforms each element using the result as the key in a map to group elements by the result + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by * * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun Array.groupBy(result: Map> = HashMap>(), toKey: (T) -> K) : Map> { +public inline fun Array.groupBy(toKey: (T) -> K) : Map> = groupByTo(HashMap>(), toKey) + +/** + * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * + * @includeFunctionBody ../../test/CollectionTest.kt groupBy + */ +public inline fun Array.groupByTo(result: Map>, toKey: (T) -> K) : Map> { for (element in this) { val key = toKey(element) val list = result.getOrPut(key) { ArrayList() } @@ -160,7 +167,7 @@ public inline fun Array.groupBy(result: Map> = HashMap Array.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String { val buffer = StringBuilder() diff --git a/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt b/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt index be372c4cbd9..a5f246eb0cf 100644 --- a/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt @@ -139,11 +139,18 @@ public inline fun java.util.Iterator.fold(initial: T, operation: (T, T) - public inline fun java.util.Iterator.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) /** - * Transforms each element using the result as the key in a map to group elements by the result + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by * * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun java.util.Iterator.groupBy(result: Map> = HashMap>(), toKey: (T) -> K) : Map> { +public inline fun java.util.Iterator.groupBy(toKey: (T) -> K) : Map> = groupByTo(HashMap>(), toKey) + +/** + * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * + * @includeFunctionBody ../../test/CollectionTest.kt groupBy + */ +public inline fun java.util.Iterator.groupByTo(result: Map>, toKey: (T) -> K) : Map> { for (element in this) { val key = toKey(element) val list = result.getOrPut(key) { ArrayList() } @@ -158,7 +165,7 @@ public inline fun java.util.Iterator.groupBy(result: Map> = * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will * a special *truncated* separator (which defaults to "..." * - * @includeFunctionBody ../../test/CollectionTest.kt appendString + * @includeFunctionBody ../../test/CollectionTest.kt makeString */ public inline fun java.util.Iterator.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String { val buffer = StringBuilder() diff --git a/libraries/stdlib/src/generated/StandardFromJLangIterables.kt b/libraries/stdlib/src/generated/StandardFromJLangIterables.kt index a07ee5d7a3a..037ebe1ba58 100644 --- a/libraries/stdlib/src/generated/StandardFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/StandardFromJLangIterables.kt @@ -141,11 +141,18 @@ public inline fun Iterable.fold(initial: T, operation: (T, T) -> T): T { public inline fun Iterable.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) /** - * Transforms each element using the result as the key in a map to group elements by the result + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by * * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun Iterable.groupBy(result: Map> = HashMap>(), toKey: (T) -> K) : Map> { +public inline fun Iterable.groupBy(toKey: (T) -> K) : Map> = groupByTo(HashMap>(), toKey) + +/** + * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * + * @includeFunctionBody ../../test/CollectionTest.kt groupBy + */ +public inline fun Iterable.groupByTo(result: Map>, toKey: (T) -> K) : Map> { for (element in this) { val key = toKey(element) val list = result.getOrPut(key) { ArrayList() } @@ -160,7 +167,7 @@ public inline fun Iterable.groupBy(result: Map> = HashMap Iterable.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String { val buffer = StringBuilder() diff --git a/libraries/stdlib/src/kotlin/JLangIterables.kt b/libraries/stdlib/src/kotlin/JLangIterables.kt index 061fafa1eb1..63a3400fbda 100644 --- a/libraries/stdlib/src/kotlin/JLangIterables.kt +++ b/libraries/stdlib/src/kotlin/JLangIterables.kt @@ -138,11 +138,18 @@ public inline fun java.lang.Iterable.fold(initial: T, operation: (T, T) - public inline fun java.lang.Iterable.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) /** - * Transforms each element using the result as the key in a map to group elements by the result + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by * * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun java.lang.Iterable.groupBy(result: Map> = HashMap>(), toKey: (T) -> K) : Map> { +public inline fun java.lang.Iterable.groupBy(toKey: (T) -> K) : Map> = groupByTo(HashMap>(), toKey) + +/** + * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * + * @includeFunctionBody ../../test/CollectionTest.kt groupBy + */ +public inline fun java.lang.Iterable.groupByTo(result: Map>, toKey: (T) -> K) : Map> { for (element in this) { val key = toKey(element) val list = result.getOrPut(key) { ArrayList() } diff --git a/libraries/stdlib/test/CollectionTest.kt b/libraries/stdlib/test/CollectionTest.kt index 3d0f60b3567..069973acbee 100644 --- a/libraries/stdlib/test/CollectionTest.kt +++ b/libraries/stdlib/test/CollectionTest.kt @@ -120,9 +120,9 @@ class CollectionTest { } } + // TODO would be nice to avoid the test fun filterIntoSet() { val data = arrayList("foo", "bar") - // TODO would be nice to avoid the val foo = data.filterTo(hashSet()){it.startsWith("f")} assertTrue { @@ -136,9 +136,9 @@ class CollectionTest { } } + // TODO would be nice to avoid the test fun filterIntoSortedSet() { val data = arrayList("foo", "bar") - // TODO would be nice to avoid the val sorted = data.filterTo(sortedSet()){it.length == 3} assertEquals(2, sorted.size) assertEquals(sortedSet("bar", "foo"), sorted) @@ -176,59 +176,39 @@ class CollectionTest { assertEquals(6, count) } - - /* - // TODO would be nice to be able to write this as this - //numbers.fold(0){it + it2} - numbers.fold(0){(it, it2) -> it + it2} - - // TODO would be nice to be able to write this as this - // numbers.map{it.toString()}.fold(""){it + it2} - numbers.map{it.toString()}.fold(""){(it, it2) -> it + it2} - */ test fun fold() { // lets calculate the sum of some numbers expect(10) { val numbers = arrayList(1, 2, 3, 4) - numbers.fold(0){(it, it2) -> it + it2} + numbers.fold(0){ a, b -> a + b} } expect(0) { val numbers = arrayList() - numbers.fold(0){(it, it2) -> it + it2} + numbers.fold(0){ a, b -> a + b} } // lets concatenate some strings expect("1234") { val numbers = arrayList(1, 2, 3, 4) - numbers.map{it.toString()}.fold(""){(it, it2) -> it + it2} + numbers.map{it.toString()}.fold(""){ a, b -> a + b} } } - /* - // TODO would be nice to be able to write this as this - // numbers.map{it.toString()}.foldRight(""){it + it2} - numbers.map{it.toString()}.foldRight(""){(it, it2) -> it + it2} - */ test fun foldRight() { expect("4321") { val numbers = arrayList(1, 2, 3, 4) - numbers.map{it.toString()}.foldRight(""){(it, it2) -> it + it2} + numbers.map{it.toString()}.foldRight(""){ a, b -> a + b} } } - /* - TODO inference engine should not need this type info? - val byLength = words.groupBy{it.length} - */ test fun groupBy() { val words = arrayList("a", "ab", "abc", "def", "abcd") - val byLength = words.groupBy{it.length} + val byLength = words.groupBy{ it.length } assertEquals(4, byLength.size()) val l3 = byLength.getOrElse(3, {ArrayList()}) assertEquals(2, l3.size) - } test fun makeString() { diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt index 82a4981e10f..fce3310a7c2 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt @@ -136,14 +136,14 @@ fun inheritedExtensionProperties(properties: Collection): Map): Map> { val map = TreeMap>() - functions.filter{ it.extensionClass != null }.groupBy(map){ it.extensionClass.sure() } + functions.filter{ it.extensionClass != null }.groupByTo(map){ it.extensionClass.sure() } return map } // TODO for some reason the SortedMap causes kotlin to freak out a little :) fun extensionProperties(properties: Collection): Map> { val map = TreeMap>() - properties.filter{ it.extensionClass != null }.groupBy(map){ it.extensionClass.sure() } + properties.filter{ it.extensionClass != null }.groupByTo(map){ it.extensionClass.sure() } return map } @@ -801,7 +801,7 @@ class KPackage(model: KModel, val descriptor: NamespaceDescriptor, } fun groupClassMap(): Map> { - return classes.groupBy(TreeMap>()){it.group} + return classes.groupByTo(TreeMap>()){it.group} } fun packageFunctions() = functions.filter{ it.extensionClass == null }