From e16a0ba650e9753d8c7e362619504ed508fd0a0f Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 4 Nov 2017 00:47:41 +0300 Subject: [PATCH] Improve and group samples for operations on Iterables #KT-20357 --- .../test/samples/collections/iterables.kt | 48 +++++++++++++------ .../src/kotlin/collections/Iterables.kt | 6 +-- .../experimental/SequenceBuilder.kt | 1 + 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/libraries/stdlib/samples/test/samples/collections/iterables.kt b/libraries/stdlib/samples/test/samples/collections/iterables.kt index f6d00c5148b..6e4ed8c8e3a 100644 --- a/libraries/stdlib/samples/test/samples/collections/iterables.kt +++ b/libraries/stdlib/samples/test/samples/collections/iterables.kt @@ -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])") } } \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/collections/Iterables.kt b/libraries/stdlib/src/kotlin/collections/Iterables.kt index 3c6f59cf32e..5af450504b4 100644 --- a/libraries/stdlib/src/kotlin/collections/Iterables.kt +++ b/libraries/stdlib/src/kotlin/collections/Iterables.kt @@ -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 Iterable(crossinline iterator: () -> Iterator): Iterable = object : Iterable { @@ -74,7 +74,7 @@ internal fun Iterable.convertToSetForSetOperation(): Collection = /** * 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 Iterable>.flatten(): List { val result = ArrayList() @@ -88,7 +88,7 @@ public fun Iterable>.flatten(): List { * 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 Iterable>.unzip(): Pair, List> { val expectedSize = collectionSizeOrDefault(10) diff --git a/libraries/stdlib/src/kotlin/coroutines/experimental/SequenceBuilder.kt b/libraries/stdlib/src/kotlin/coroutines/experimental/SequenceBuilder.kt index 32f2d304c8c..2f4f35f23c8 100644 --- a/libraries/stdlib/src/kotlin/coroutines/experimental/SequenceBuilder.kt +++ b/libraries/stdlib/src/kotlin/coroutines/experimental/SequenceBuilder.kt @@ -35,6 +35,7 @@ public fun buildSequence(builderAction: suspend SequenceBuilder.() -> 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 buildIterator(builderAction: suspend SequenceBuilder.() -> Unit): Iterator {