Improve and group samples for operations on Iterables #KT-20357
This commit is contained in:
@@ -20,24 +20,44 @@ import samples.*
|
||||
import kotlin.coroutines.experimental.buildIterator
|
||||
import kotlin.test.*
|
||||
|
||||
@RunWith(Enclosed::class)
|
||||
class Iterables {
|
||||
|
||||
@Sample
|
||||
fun iterable() {
|
||||
val iterable = Iterable { buildIterator { yieldAll(1..3) } }
|
||||
val result = iterable.mapIndexed { index, i -> index + i }
|
||||
assertPrints(result, "[1, 3, 5]")
|
||||
class Building {
|
||||
|
||||
@Sample
|
||||
fun iterable() {
|
||||
val iterable = Iterable {
|
||||
buildIterator {
|
||||
yield(42)
|
||||
yieldAll(1..5 step 2)
|
||||
}
|
||||
}
|
||||
val result = iterable.mapIndexed { index, value -> "$index: $value" }
|
||||
assertPrints(result, "[0: 42, 1: 1, 2: 3, 3: 5]")
|
||||
|
||||
// can be iterated many times
|
||||
repeat(2) {
|
||||
val sum = iterable.sum()
|
||||
assertPrints(sum, "51")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun flattenIterable() {
|
||||
val deepList = listOf(listOf(1), listOf(2, 3), listOf(4, 5, 6))
|
||||
assertPrints(deepList.flatten(), "[1, 2, 3, 4, 5, 6]")
|
||||
}
|
||||
class Operations {
|
||||
|
||||
@Sample
|
||||
fun flattenIterable() {
|
||||
val deepList = listOf(listOf(1), listOf(2, 3), listOf(4, 5, 6))
|
||||
assertPrints(deepList.flatten(), "[1, 2, 3, 4, 5, 6]")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun unzipIterable() {
|
||||
val list = listOf(1 to 'a', 2 to 'b', 3 to 'c')
|
||||
assertPrints(list.unzip(), "([1, 2, 3], [a, b, c])")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun unzipIterable() {
|
||||
val list = listOf(1 to 'a', 2 to 'b', 3 to 'c')
|
||||
assertPrints(list.unzip(), "([1, 2, 3], [a, b, c])")
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ package kotlin.collections
|
||||
/**
|
||||
* Given an [iterator] function constructs an [Iterable] instance that returns values through the [Iterator]
|
||||
* provided by that function.
|
||||
* @sample samples.collections.Iterables.iterable
|
||||
* @sample samples.collections.Iterables.Building.iterable
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Iterable(crossinline iterator: () -> Iterator<T>): Iterable<T> = object : Iterable<T> {
|
||||
@@ -74,7 +74,7 @@ internal fun <T> Iterable<T>.convertToSetForSetOperation(): Collection<T> =
|
||||
|
||||
/**
|
||||
* Returns a single list of all elements from all collections in the given collection.
|
||||
* @sample samples.collections.Iterables.flattenIterable
|
||||
* @sample samples.collections.Iterables.Operations.flattenIterable
|
||||
*/
|
||||
public fun <T> Iterable<Iterable<T>>.flatten(): List<T> {
|
||||
val result = ArrayList<T>()
|
||||
@@ -88,7 +88,7 @@ public fun <T> Iterable<Iterable<T>>.flatten(): List<T> {
|
||||
* Returns a pair of lists, where
|
||||
* *first* list is built from the first values of each pair from this collection,
|
||||
* *second* list is built from the second values of each pair from this collection.
|
||||
* @sample samples.collections.Iterables.unzipIterable
|
||||
* @sample samples.collections.Iterables.Operations.unzipIterable
|
||||
*/
|
||||
public fun <T, R> Iterable<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
|
||||
val expectedSize = collectionSizeOrDefault(10)
|
||||
|
||||
@@ -35,6 +35,7 @@ public fun <T> buildSequence(builderAction: suspend SequenceBuilder<T>.() -> Uni
|
||||
* Builds an [Iterator] lazily yielding values one by one.
|
||||
*
|
||||
* @sample samples.collections.Sequences.Building.buildIterator
|
||||
* @sample samples.collections.Iterables.Building.iterable
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
public fun <T> buildIterator(builderAction: suspend SequenceBuilder<T>.() -> Unit): Iterator<T> {
|
||||
|
||||
Reference in New Issue
Block a user