diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 475e0d436be..9bc85a82d71 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -6456,174 +6456,453 @@ public inline fun > ShortArray.flatMapTo(destinat } /** - * Returns a map of the elements in original array grouped by the key returned by the given [selector] function. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun Array.groupBy(selector: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun Array.groupBy(keySelector: (T) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Returns a map of the elements in original array grouped by the key returned by the given [selector] function. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun BooleanArray.groupBy(selector: (Boolean) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Returns a map of the elements in original array grouped by the key returned by the given [selector] function. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun ByteArray.groupBy(selector: (Byte) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun ByteArray.groupBy(keySelector: (Byte) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Returns a map of the elements in original array grouped by the key returned by the given [selector] function. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun CharArray.groupBy(selector: (Char) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun CharArray.groupBy(keySelector: (Char) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Returns a map of the elements in original array grouped by the key returned by the given [selector] function. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun DoubleArray.groupBy(selector: (Double) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun DoubleArray.groupBy(keySelector: (Double) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Returns a map of the elements in original array grouped by the key returned by the given [selector] function. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun FloatArray.groupBy(selector: (Float) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun FloatArray.groupBy(keySelector: (Float) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Returns a map of the elements in original array grouped by the key returned by the given [selector] function. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun IntArray.groupBy(selector: (Int) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun IntArray.groupBy(keySelector: (Int) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Returns a map of the elements in original array grouped by the key returned by the given [selector] function. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun LongArray.groupBy(selector: (Long) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun LongArray.groupBy(keySelector: (Long) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Returns a map of the elements in original array grouped by the key returned by the given [selector] function. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun ShortArray.groupBy(selector: (Short) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun ShortArray.groupBy(keySelector: (Short) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Appends elements from original array grouped by the key returned by the given [selector] function to the given [map]. + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues */ -public inline fun Array.groupByTo(map: MutableMap>, selector: (T) -> K): Map> { +public inline fun Array.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun ByteArray.groupBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun CharArray.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun DoubleArray.groupBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun FloatArray.groupBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun IntArray.groupBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun LongArray.groupBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun ShortArray.groupBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy + */ +public inline fun >> Array.groupByTo(destination: M, keySelector: (T) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination } /** - * Appends elements from original array grouped by the key returned by the given [selector] function to the given [map]. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun BooleanArray.groupByTo(map: MutableMap>, selector: (Boolean) -> K): Map> { +public inline fun >> BooleanArray.groupByTo(destination: M, keySelector: (Boolean) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination } /** - * Appends elements from original array grouped by the key returned by the given [selector] function to the given [map]. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun ByteArray.groupByTo(map: MutableMap>, selector: (Byte) -> K): Map> { +public inline fun >> ByteArray.groupByTo(destination: M, keySelector: (Byte) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination } /** - * Appends elements from original array grouped by the key returned by the given [selector] function to the given [map]. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun CharArray.groupByTo(map: MutableMap>, selector: (Char) -> K): Map> { +public inline fun >> CharArray.groupByTo(destination: M, keySelector: (Char) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination } /** - * Appends elements from original array grouped by the key returned by the given [selector] function to the given [map]. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun DoubleArray.groupByTo(map: MutableMap>, selector: (Double) -> K): Map> { +public inline fun >> DoubleArray.groupByTo(destination: M, keySelector: (Double) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination } /** - * Appends elements from original array grouped by the key returned by the given [selector] function to the given [map]. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun FloatArray.groupByTo(map: MutableMap>, selector: (Float) -> K): Map> { +public inline fun >> FloatArray.groupByTo(destination: M, keySelector: (Float) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination } /** - * Appends elements from original array grouped by the key returned by the given [selector] function to the given [map]. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun IntArray.groupByTo(map: MutableMap>, selector: (Int) -> K): Map> { +public inline fun >> IntArray.groupByTo(destination: M, keySelector: (Int) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination } /** - * Appends elements from original array grouped by the key returned by the given [selector] function to the given [map]. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun LongArray.groupByTo(map: MutableMap>, selector: (Long) -> K): Map> { +public inline fun >> LongArray.groupByTo(destination: M, keySelector: (Long) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination } /** - * Appends elements from original array grouped by the key returned by the given [selector] function to the given [map]. + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun ShortArray.groupByTo(map: MutableMap>, selector: (Short) -> K): Map> { +public inline fun >> ShortArray.groupByTo(destination: M, keySelector: (Short) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> Array.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> BooleanArray.groupByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> ByteArray.groupByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> CharArray.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> DoubleArray.groupByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> FloatArray.groupByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> IntArray.groupByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> LongArray.groupByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> ShortArray.groupByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination } /** diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 9b7c3ed7128..f34cf90a1ee 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -1045,22 +1045,53 @@ public inline fun > Iterable.flatMapTo(dest } /** - * Returns a map of the elements in original collection grouped by the key returned by the given [selector] function. + * Groups elements of the original collection by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun Iterable.groupBy(selector: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun Iterable.groupBy(keySelector: (T) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Appends elements from original collection grouped by the key returned by the given [selector] function to the given [map]. + * Groups values returned by the [valueTransform] function applied to each element of the original collection + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues */ -public inline fun Iterable.groupByTo(map: MutableMap>, selector: (T) -> K): Map> { +public inline fun Iterable.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups elements of the original collection by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy + */ +public inline fun >> Iterable.groupByTo(destination: M, keySelector: (T) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original collection + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> Iterable.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination } /** diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 038c871c1c6..dd2586af528 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -542,22 +542,53 @@ public inline fun > Sequence.flatMapTo(dest } /** - * Returns a map of the elements in original sequence grouped by the key returned by the given [selector] function. + * Groups elements of the original sequence by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun Sequence.groupBy(selector: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun Sequence.groupBy(keySelector: (T) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Appends elements from original sequence grouped by the key returned by the given [selector] function to the given [map]. + * Groups values returned by the [valueTransform] function applied to each element of the original sequence + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues */ -public inline fun Sequence.groupByTo(map: MutableMap>, selector: (T) -> K): Map> { +public inline fun Sequence.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups elements of the original sequence by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy + */ +public inline fun >> Sequence.groupByTo(destination: M, keySelector: (T) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original sequence + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> Sequence.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination } /** diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index 1fd611841c0..04391b16923 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -605,22 +605,53 @@ public inline fun > CharSequence.flatMapTo(destin } /** - * Returns a map of the characters in original char sequence grouped by the key returned by the given [selector] function. + * Groups characters of the original char sequence by the key returned by the given [keySelector] function + * applied to each character and returns a map where each group key is associated with a list of corresponding characters. + * @sample test.collections.CollectionTest.groupBy */ -public inline fun CharSequence.groupBy(selector: (Char) -> K): Map> { - return groupByTo(LinkedHashMap>(), selector) +public inline fun CharSequence.groupBy(keySelector: (Char) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) } /** - * Appends characters from original char sequence grouped by the key returned by the given [selector] function to the given [map]. + * Groups values returned by the [valueTransform] function applied to each character of the original char sequence + * by the key returned by the given [keySelector] function applied to the character + * and returns a map where each group key is associated with a list of corresponding values. + * @sample test.collections.CollectionTest.groupByKeysAndValues */ -public inline fun CharSequence.groupByTo(map: MutableMap>, selector: (Char) -> K): Map> { +public inline fun CharSequence.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups characters of the original char sequence by the key returned by the given [keySelector] function + * applied to each character and puts to the [destination] map each group key associated with a list of corresponding characters. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupBy + */ +public inline fun >> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K): M { for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each character of the original char sequence + * by the key returned by the given [keySelector] function applied to the character + * and puts to the [destination] map each group key associated with a list of corresponding values. + * @return The [destination] map. + * @sample test.collections.CollectionTest.groupByKeysAndValues + */ +public inline fun >> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination } /** diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index 11fbf81e73f..5878a43ee81 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -192,14 +192,30 @@ class CollectionTest { assertEquals(4, byLength.size) // verify that order of keys is preserved - val listOfPairs = byLength.toList() - assertEquals(1, listOfPairs[0].first) - assertEquals(3, listOfPairs[1].first) - assertEquals(2, listOfPairs[2].first) - assertEquals(4, listOfPairs[3].first) + assertEquals(listOf( + 1 to listOf("a"), + 3 to listOf("abc", "def"), + 2 to listOf("ab"), + 4 to listOf("abcd") + ), byLength.toList()) - val l3 = byLength.getOrElse(3, { ArrayList() }) - assertEquals(2, l3.size) + val l3 = byLength[3].orEmpty() + assertEquals(listOf("abc", "def"), l3) + } + + @test fun groupByKeysAndValues() { + val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing") + val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first }) + assertEquals( + listOf( + "Marketing" to listOf("Alice", "Carol"), + "Sales" to listOf("Bob") + ), + namesByTeam.toList()) + + + val mutableNamesByTeam = nameToTeam.groupByTo(HashMap(), { it.second }, { it.first }) + assertEquals(namesByTeam, mutableNamesByTeam) } @test fun plusRanges() { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt index d5bd8470e67..6db8d27f5c7 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt @@ -277,33 +277,98 @@ fun mapping(): List { } } - templates add f("groupBy(selector: (T) -> K)") { + templates add f("groupBy(keySelector: (T) -> K)") { inline(true) include(CharSequences) - doc { f -> "Returns a map of the ${f.element.pluralize()} in original ${f.collection} grouped by the key returned by the given [selector] function." } + doc { f -> + """ + Groups ${f.element.pluralize()} of the original ${f.collection} by the key returned by the given [keySelector] function + applied to each ${f.element} and returns a map where each group key is associated with a list of corresponding ${f.element.pluralize()}. + @sample test.collections.CollectionTest.groupBy + """ + } typeParam("K") returns("Map>") - body { "return groupByTo(LinkedHashMap>(), selector)" } + body { "return groupByTo(LinkedHashMap>(), keySelector)" } } - templates add f("groupByTo(map: MutableMap>, selector: (T) -> K)") { + templates add f("groupByTo(destination: M, keySelector: (T) -> K)") { inline(true) include(CharSequences) typeParam("K") - doc { f -> "Appends ${f.element.pluralize()} from original ${f.collection} grouped by the key returned by the given [selector] function to the given [map]." } - returns("Map>") + typeParam("M : MutableMap>") + doc { f -> + """ + Groups ${f.element.pluralize()} of the original ${f.collection} by the key returned by the given [keySelector] function + applied to each ${f.element} and puts to the [destination] map each group key associated with a list of corresponding ${f.element.pluralize()}. + + @return The [destination] map. + @sample test.collections.CollectionTest.groupBy + """ + } + returns("M") body { """ for (element in this) { - val key = selector(element) - val list = map.getOrPut(key) { ArrayList() } + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } list.add(element) } - return map + return destination """ } } + + templates add f("groupBy(keySelector: (T) -> K, valueTransform: (T) -> V)") { + inline(true) + + include(CharSequences) + doc { f -> + """ + Groups values returned by the [valueTransform] function applied to each ${f.element} of the original ${f.collection} + by the key returned by the given [keySelector] function applied to the ${f.element} + and returns a map where each group key is associated with a list of corresponding values. + @sample test.collections.CollectionTest.groupByKeysAndValues + """ + } + typeParam("K") + typeParam("V") + returns("Map>") + body { "return groupByTo(LinkedHashMap>(), keySelector, valueTransform)" } + } + + + templates add f("groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") { + inline(true) + + include(CharSequences) + typeParam("K") + typeParam("V") + typeParam("M : MutableMap>") + + doc { f -> + """ + Groups values returned by the [valueTransform] function applied to each ${f.element} of the original ${f.collection} + by the key returned by the given [keySelector] function applied to the ${f.element} + and puts to the [destination] map each group key associated with a list of corresponding values. + @return The [destination] map. + @sample test.collections.CollectionTest.groupByKeysAndValues + """ + } + returns("M") + body { + """ + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination + """ + } + } + return templates } \ No newline at end of file