From 0eae27e03fc0ab00e549e00c45790f2c2c564e50 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 19 Apr 2018 21:16:11 +0300 Subject: [PATCH] Add samples for zip on sequences and strings --- .../stdlib/common/src/generated/_Sequences.kt | 4 ++++ .../stdlib/common/src/generated/_Strings.kt | 4 ++++ .../test/samples/collections/sequences.kt | 17 +++++++++++++++++ .../stdlib/samples/test/samples/text/strings.kt | 15 +++++++++++++++ .../src/templates/Generators.kt | 8 ++++++++ 5 files changed, 48 insertions(+) diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index 0974ac6f628..f6102a6d319 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -1559,6 +1559,8 @@ public fun Sequence.windowed(size: Int, step: Int = 1, partialWindows: /** * Returns a sequence of pairs built from elements of both sequences with same indexes. * Resulting sequence has length of shortest input sequence. + * + * @sample samples.collections.Sequences.Transformations.zip * * The operation is _intermediate_ and _stateless_. */ @@ -1568,6 +1570,8 @@ public infix fun Sequence.zip(other: Sequence): Sequence /** * Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. + * + * @sample samples.collections.Sequences.Transformations.zipWithTransform * * The operation is _intermediate_ and _stateless_. */ diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index 4a39409b184..f2bb25b0dcc 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -1313,6 +1313,8 @@ public fun CharSequence.windowedSequence(size: Int, step: Int = 1, partialWi /** * Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence. + * + * @sample samples.text.Strings.zip */ public infix fun CharSequence.zip(other: CharSequence): List> { return zip(other) { c1, c2 -> c1 to c2 } @@ -1320,6 +1322,8 @@ public infix fun CharSequence.zip(other: CharSequence): List> { /** * Returns a list of values built from characters of both char sequences with same indexes using provided [transform]. List has length of shortest char sequence. + * + * @sample samples.text.Strings.zipWithTransform */ public inline fun CharSequence.zip(other: CharSequence, transform: (a: Char, b: Char) -> V): List { val length = minOf(this.length, other.length) diff --git a/libraries/stdlib/samples/test/samples/collections/sequences.kt b/libraries/stdlib/samples/test/samples/collections/sequences.kt index 8196b0f8c48..3568596dec3 100644 --- a/libraries/stdlib/samples/test/samples/collections/sequences.kt +++ b/libraries/stdlib/samples/test/samples/collections/sequences.kt @@ -188,6 +188,23 @@ class Sequences { val averagedNoPartialWindows = dataPoints.windowed(size = 4, step = 1).map { it.average() } assertPrints(averagedNoPartialWindows.toList(), "[17.0, 19.25, 20.75, 19.75, 15.5, 12.0]") } + + @Sample + fun zip() { + val sequenceA = ('a'..'z').asSequence() + val sequenceB = generateSequence(1) { it * 2 + 1 } + + assertPrints((sequenceA zip sequenceB).take(4).toList(), "[(a, 1), (b, 3), (c, 7), (d, 15)]") + } + + @Sample + fun zipWithTransform() { + val sequenceA = ('a'..'z').asSequence() + val sequenceB = generateSequence(1) { it * 2 + 1 } + + val result = sequenceA.zip(sequenceB) { a, b -> "$a/$b" } + assertPrints(result.take(4).toList(), "[a/1, b/3, c/7, d/15]") + } } } \ No newline at end of file diff --git a/libraries/stdlib/samples/test/samples/text/strings.kt b/libraries/stdlib/samples/test/samples/text/strings.kt index 7534bdfe5e7..79675c04e93 100644 --- a/libraries/stdlib/samples/test/samples/text/strings.kt +++ b/libraries/stdlib/samples/test/samples/text/strings.kt @@ -71,6 +71,21 @@ class Strings { assertPrints(proteins.take(5).toList(), "[Isoleucine, Arginine, Glycine, Arginine, Glutamine]") } + @Sample + fun zip() { + val stringA = "abcd" + val stringB = "zyx" + assertPrints(stringA zip stringB, "[(a, z), (b, y), (c, x)]") + } + + @Sample + fun zipWithTransform() { + val stringA = "abcd" + val stringB = "zyx" + val result = stringA.zip(stringB) { a, b -> "$a$b" } + assertPrints(result, "[az, by, cx]") + } + @Sample fun stringToByteArray() { val charset = Charsets.UTF_8 diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index bd17d518069..d352a4c3e0a 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -1108,6 +1108,8 @@ object Generators : TemplateGroupBase() { doc { """ Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. + + @sample samples.collections.Sequences.Transformations.zipWithTransform """ } sequenceClassification(intermediate, stateless) @@ -1127,6 +1129,8 @@ object Generators : TemplateGroupBase() { doc { """ Returns a list of values built from characters of both char sequences with same indexes using provided [transform]. List has length of shortest char sequence. + + @sample samples.text.Strings.zipWithTransform """ } typeParam("V") @@ -1173,6 +1177,8 @@ object Generators : TemplateGroupBase() { doc { """ Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence. + + @sample samples.text.Strings.zip """ } returns("List>") @@ -1230,6 +1236,8 @@ object Generators : TemplateGroupBase() { """ Returns a sequence of pairs built from elements of both sequences with same indexes. Resulting sequence has length of shortest input sequence. + + @sample samples.collections.Sequences.Transformations.zip """ } sequenceClassification(intermediate, stateless)