Move groupingByEachCount together with the new Grouping samples

This commit is contained in:
Ilya Gorbunov
2018-09-28 18:15:48 +03:00
parent 039d41679e
commit 3a40e3f041
9 changed files with 21 additions and 17 deletions
@@ -8668,7 +8668,7 @@ public inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharArray.groupBy
* 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.
*
* @sample samples.collections.Collections.Transformations.groupingByEachCount
* @sample samples.collections.Grouping.groupingByEachCount
*/
@SinceKotlin("1.1")
public inline fun <T, K> Array<out T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K> {
@@ -1270,7 +1270,7 @@ public inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Iterable<T>.gr
* 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.
*
* @sample samples.collections.Collections.Transformations.groupingByEachCount
* @sample samples.collections.Grouping.groupingByEachCount
*/
@SinceKotlin("1.1")
public inline fun <T, K> Iterable<T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K> {
@@ -828,7 +828,7 @@ public inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Sequence<T>.gr
*
* The operation is _intermediate_ and _stateless_.
*
* @sample samples.collections.Collections.Transformations.groupingByEachCount
* @sample samples.collections.Grouping.groupingByEachCount
*/
@SinceKotlin("1.1")
public inline fun <T, K> Sequence<T>.groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K> {
@@ -805,7 +805,7 @@ public inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharSequence.grou
* 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.
*
* @sample samples.collections.Collections.Transformations.groupingByEachCount
* @sample samples.collections.Grouping.groupingByEachCount
*/
@SinceKotlin("1.1")
public inline fun <K> CharSequence.groupingBy(crossinline keySelector: (Char) -> K): Grouping<Char, K> {
@@ -14,7 +14,7 @@ package kotlin.collections
*
* @return a [Map] associating the key of each group with the count of elements in the group.
*
* @sample samples.collections.Collections.Transformations.groupingByEachCount
* @sample samples.collections.Grouping.groupingByEachCount
*/
@SinceKotlin("1.1")
public actual fun <T, K> Grouping<T, K>.eachCount(): Map<K, Int> =
@@ -369,17 +369,7 @@ class Collections {
assertTrue(mutableNamesByTeam == namesByTeam)
}
@Sample
fun groupingByEachCount() {
val words = "one two three four five six seven eight nine ten".split(' ')
val frequenciesByFirstChar = words.groupingBy { it.first() }.eachCount()
println("Counting first letters:")
assertPrints(frequenciesByFirstChar, "{o=1, t=3, f=2, s=2, e=1, n=1}")
val moreWords = "eleven twelve".split(' ')
val moreFrequencies = moreWords.groupingBy { it.first() }.eachCountTo(frequenciesByFirstChar.toMutableMap())
assertPrints(moreFrequencies, "{o=1, t=4, f=2, s=2, e=2, n=1}")
}
@Sample
fun joinTo() {
@@ -10,6 +10,20 @@ import java.util.stream.Collector
import kotlin.test.*
class Grouping {
@Sample
fun groupingByEachCount() {
val words = "one two three four five six seven eight nine ten".split(' ')
val frequenciesByFirstChar = words.groupingBy { it.first() }.eachCount()
println("Counting first letters:")
assertPrints(frequenciesByFirstChar, "{o=1, t=3, f=2, s=2, e=1, n=1}")
val moreWords = "eleven twelve".split(' ')
val moreFrequencies = moreWords.groupingBy { it.first() }.eachCountTo(frequenciesByFirstChar.toMutableMap())
assertPrints(moreFrequencies, "{o=1, t=4, f=2, s=2, e=2, n=1}")
}
@Sample
fun aggregateEvenAndOdd() {
val intList = listOf(0, 1, 2, 3, 4, 5, 6)
@@ -250,7 +250,7 @@ public inline fun <S, T : S, K, M : MutableMap<in K, S>> Grouping<T, K>.reduceTo
*
* @return the [destination] map associating the key of each group with the count of elements in the group.
*
* @sample samples.collections.Collections.Transformations.groupingByEachCount
* @sample samples.collections.Grouping.groupingByEachCount
*/
@SinceKotlin("1.1")
public fun <T, K, M : MutableMap<in K, Int>> Grouping<T, K>.eachCountTo(destination: M): M =
@@ -429,7 +429,7 @@ object Mapping : TemplateGroupBase() {
using the specified [keySelector] function to extract a key from each ${f.element}.
"""
}
sample("samples.collections.Collections.Transformations.groupingByEachCount")
sample("samples.collections.Grouping.groupingByEachCount")
body {
"""