Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

group-and-fold implementation #1004

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions js/js.libraries/src/core/generated/_ArraysJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7630,6 +7630,18 @@ public inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharArray.groupBy
return destination
}

/**
* Creates a [Grouping] source from an array to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each element.
*/
@SinceKotlin("1.1")
public inline fun <T, K> Array<out T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K> {
return object : Grouping<T, K> {
override fun sourceIterator(): Iterator<T> = [email protected]()
override fun keyOf(element: T): K = keySelector(element)
}
}

/**
* Returns a list containing the results of applying the given [transform] function
* to each element in the original array.
Expand Down
12 changes: 12 additions & 0 deletions js/js.libraries/src/core/generated/_CollectionsJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,18 @@ public inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Iterable<T>.gr
return destination
}

/**
* Creates a [Grouping] source from a collection to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each element.
*/
@SinceKotlin("1.1")
public inline fun <T, K> Iterable<T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K> {
return object : Grouping<T, K> {
override fun sourceIterator(): Iterator<T> = [email protected]()
override fun keyOf(element: T): K = keySelector(element)
}
}

/**
* Returns a list containing the results of applying the given [transform] function
* to each element in the original collection.
Expand Down
12 changes: 12 additions & 0 deletions js/js.libraries/src/core/generated/_SequencesJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,18 @@ public inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Sequence<T>.gr
return destination
}

/**
* Creates a [Grouping] source from a sequence to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each element.
*/
@SinceKotlin("1.1")
public inline fun <T, K> Sequence<T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K> {
return object : Grouping<T, K> {
override fun sourceIterator(): Iterator<T> = [email protected]()
override fun keyOf(element: T): K = keySelector(element)
}
}

/**
* Returns a sequence containing the results of applying the given [transform] function
* to each element in the original sequence.
Expand Down
12 changes: 12 additions & 0 deletions js/js.libraries/src/core/generated/_StringsJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,18 @@ public inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharSequence.grou
return destination
}

/**
* Creates a [Grouping] source from a char sequence to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each character.
*/
@SinceKotlin("1.1")
public inline fun <K> CharSequence.groupingBy(crossinline keySelector: (Char) -> K): Grouping<Char, K> {
return object : Grouping<Char, K> {
override fun sourceIterator(): Iterator<Char> = [email protected]()
override fun keyOf(element: Char): K = keySelector(element)
}
}

/**
* Returns a list containing the results of applying the given [transform] function
* to each character in the original char sequence.
Expand Down
34 changes: 34 additions & 0 deletions js/js.libraries/src/core/grouping.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.collections

/**
* Groups elements from the [Grouping] source by key and counts elements in each group.
*
* @return a [Map] associating the key of each group with the count of element in the group.
*/
@SinceKotlin("1.1")
public fun <T, K> Grouping<T, K>.eachCount(): Map<K, Int> =
fold(0) { acc, e -> acc + 1 }

/**
* Groups elements from the [Grouping] source by key and sums values provided by the [valueSelector] function for elements in each group.
*
* @return a [Map] associating the key of each group with the count of element in the group.
*/
@SinceKotlin("1.1")
public inline fun <T, K> Grouping<T, K>.eachSumOf(valueSelector: (T) -> Int): Map<K, Int> =
fold(0) { acc, e -> acc + valueSelector(e) }
7 changes: 7 additions & 0 deletions libraries/stdlib/common/src/generated/_Arrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4445,6 +4445,13 @@ public header inline fun <K, V, M : MutableMap<in K, MutableList<V>>> BooleanArr
*/
public header inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharArray.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M

/**
* Creates a [Grouping] source from an array to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each element.
*/
@SinceKotlin("1.1")
public header inline fun <T, K> Array<out T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K>

/**
* Returns a list containing the results of applying the given [transform] function
* to each element in the original array.
Expand Down
7 changes: 7 additions & 0 deletions libraries/stdlib/common/src/generated/_Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,13 @@ public header inline fun <T, K, M : MutableMap<in K, MutableList<T>>> Iterable<T
*/
public header inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Iterable<T>.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M

/**
* Creates a [Grouping] source from a collection to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each element.
*/
@SinceKotlin("1.1")
public header inline fun <T, K> Iterable<T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K>

/**
* Returns a list containing the results of applying the given [transform] function
* to each element in the original collection.
Expand Down
7 changes: 7 additions & 0 deletions libraries/stdlib/common/src/generated/_Sequences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ public header inline fun <T, K, M : MutableMap<in K, MutableList<T>>> Sequence<T
*/
public header inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Sequence<T>.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M

/**
* Creates a [Grouping] source from a sequence to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each element.
*/
@SinceKotlin("1.1")
public header inline fun <T, K> Sequence<T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K>

/**
* Returns a sequence containing the results of applying the given [transform] function
* to each element in the original sequence.
Expand Down
7 changes: 7 additions & 0 deletions libraries/stdlib/common/src/generated/_Strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,13 @@ public header inline fun <K, M : MutableMap<in K, MutableList<Char>>> CharSequen
*/
public header inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M

/**
* Creates a [Grouping] source from a char sequence to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each character.
*/
@SinceKotlin("1.1")
public header inline fun <K> CharSequence.groupingBy(crossinline keySelector: (Char) -> K): Grouping<Char, K>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why there is specialization here? the implementations look the same

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's how the code is generated now.


/**
* Returns a list containing the results of applying the given [transform] function
* to each character in the original char sequence.
Expand Down
5 changes: 5 additions & 0 deletions libraries/stdlib/common/src/kotlin/CollectionsH.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,8 @@ header fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit

// from Maps.kt
header operator fun <K, V> MutableMap<K, V>.set(key: K, value: V): Unit


// from Grouping.kt
public header fun <T, K> Grouping<T, K>.eachCount(): Map<K, Int>
public header inline fun <T, K> Grouping<T, K>.eachSumOf(valueSelector: (T) -> Int): Map<K, Int>
12 changes: 12 additions & 0 deletions libraries/stdlib/src/generated/_Arrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7713,6 +7713,18 @@ public inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharArray.groupBy
return destination
}

/**
* Creates a [Grouping] source from an array to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each element.
*/
@SinceKotlin("1.1")
public inline fun <T, K> Array<out T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K> {
return object : Grouping<T, K> {
override fun sourceIterator(): Iterator<T> = [email protected]()
override fun keyOf(element: T): K = keySelector(element)
}
}

/**
* Returns a list containing the results of applying the given [transform] function
* to each element in the original array.
Expand Down
12 changes: 12 additions & 0 deletions libraries/stdlib/src/generated/_Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,18 @@ public inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Iterable<T>.gr
return destination
}

/**
* Creates a [Grouping] source from a collection to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each element.
*/
@SinceKotlin("1.1")
public inline fun <T, K> Iterable<T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K> {
return object : Grouping<T, K> {
override fun sourceIterator(): Iterator<T> = [email protected]()
override fun keyOf(element: T): K = keySelector(element)
}
}

/**
* Returns a list containing the results of applying the given [transform] function
* to each element in the original collection.
Expand Down
12 changes: 12 additions & 0 deletions libraries/stdlib/src/generated/_Sequences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,18 @@ public inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Sequence<T>.gr
return destination
}

/**
* Creates a [Grouping] source from a sequence to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each element.
*/
@SinceKotlin("1.1")
public inline fun <T, K> Sequence<T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K> {
return object : Grouping<T, K> {
override fun sourceIterator(): Iterator<T> = [email protected]()
override fun keyOf(element: T): K = keySelector(element)
}
}

/**
* Returns a sequence containing the results of applying the given [transform] function
* to each element in the original sequence.
Expand Down
12 changes: 12 additions & 0 deletions libraries/stdlib/src/generated/_Strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,18 @@ public inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharSequence.grou
return destination
}

/**
* Creates a [Grouping] source from a char sequence to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each character.
*/
@SinceKotlin("1.1")
public inline fun <K> CharSequence.groupingBy(crossinline keySelector: (Char) -> K): Grouping<Char, K> {
return object : Grouping<Char, K> {
override fun sourceIterator(): Iterator<Char> = [email protected]()
override fun keyOf(element: Char): K = keySelector(element)
}
}

/**
* Returns a list containing the results of applying the given [transform] function
* to each character in the original char sequence.
Expand Down
Loading