Rename Grouping.elementIterator to sourceIterator
This commit is contained in:
@@ -7720,7 +7720,7 @@ public inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharArray.groupBy
|
||||
@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 elementIterator(): Iterator<T> = this@groupingBy.iterator()
|
||||
override fun sourceIterator(): Iterator<T> = this@groupingBy.iterator()
|
||||
override fun keyOf(element: T): K = keySelector(element)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1197,7 +1197,7 @@ public inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Iterable<T>.gr
|
||||
@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 elementIterator(): Iterator<T> = this@groupingBy.iterator()
|
||||
override fun sourceIterator(): Iterator<T> = this@groupingBy.iterator()
|
||||
override fun keyOf(element: T): K = keySelector(element)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -670,7 +670,7 @@ public inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Sequence<T>.gr
|
||||
@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 elementIterator(): Iterator<T> = this@groupingBy.iterator()
|
||||
override fun sourceIterator(): Iterator<T> = this@groupingBy.iterator()
|
||||
override fun keyOf(element: T): K = keySelector(element)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -717,7 +717,7 @@ public inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharSequence.grou
|
||||
@SinceKotlin("1.1")
|
||||
public inline fun <K> CharSequence.groupingBy(crossinline keySelector: (Char) -> K): Grouping<Char, K> {
|
||||
return object : Grouping<Char, K> {
|
||||
override fun elementIterator(): Iterator<Char> = this@groupingBy.iterator()
|
||||
override fun sourceIterator(): Iterator<Char> = this@groupingBy.iterator()
|
||||
override fun keyOf(element: Char): K = keySelector(element)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ package kotlin.collections
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
public interface Grouping<T, out K> {
|
||||
/** Returns an [Iterator] which iterates through the elements of the source. */
|
||||
fun elementIterator(): Iterator<T>
|
||||
/** Returns an [Iterator] over the elements of the source of this grouping. */
|
||||
fun sourceIterator(): Iterator<T>
|
||||
/** Extracts the key of an [element]. */
|
||||
fun keyOf(element: T): K
|
||||
}
|
||||
@@ -65,7 +65,7 @@ public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.aggregateTo(
|
||||
destination: M,
|
||||
operation: (key: K, accumulator: R?, element: T, first: Boolean) -> R
|
||||
): M {
|
||||
for (e in this.elementIterator()) {
|
||||
for (e in this.sourceIterator()) {
|
||||
val key = keyOf(e)
|
||||
val accumulator = destination[key]
|
||||
destination[key] = operation(key, accumulator, e, accumulator == null && !destination.containsKey(key))
|
||||
|
||||
@@ -9,7 +9,7 @@ class GroupingTest {
|
||||
@Test fun groupingProducers() {
|
||||
|
||||
fun <T, K> verifyGrouping(grouping: Grouping<T, K>, expectedElements: List<T>, expectedKeys: List<K>) {
|
||||
val elements = grouping.elementIterator().asSequence().toList()
|
||||
val elements = grouping.sourceIterator().asSequence().toList()
|
||||
val keys = elements.map { grouping.keyOf(it) } // TODO: replace with grouping::keyOf when supported in JS
|
||||
|
||||
assertEquals(expectedElements, elements)
|
||||
|
||||
Reference in New Issue
Block a user