diff --git a/js/js.libraries/src/core/generated/_CollectionsJs.kt b/js/js.libraries/src/core/generated/_CollectionsJs.kt index d5c0bb681fb..ff67ac709fb 100644 --- a/js/js.libraries/src/core/generated/_CollectionsJs.kt +++ b/js/js.libraries/src/core/generated/_CollectionsJs.kt @@ -1844,11 +1844,26 @@ public inline fun Iterable.minusElement(element: T): List { return minus(element) } +/** + * Returns a list of pairs of each two adjacent elements in this collection. + * + * The returned list is empty if this collection contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwise + */ @SinceKotlin("1.2") public fun Iterable.pairwise(): List> { return pairwise { a, b -> a to b } } +/** + * Returns a list containing the results of applying the given [transform] function + * to an each pair of two adjacent elements in this collection. + * + * The returned list is empty if this collection contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas + */ @SinceKotlin("1.2") public inline fun Iterable.pairwise(transform: (a: T, b: T) -> R): List { val iterator = iterator() diff --git a/js/js.libraries/src/core/generated/_SequencesJs.kt b/js/js.libraries/src/core/generated/_SequencesJs.kt index f2d4d8e35c9..d49e53b12f5 100644 --- a/js/js.libraries/src/core/generated/_SequencesJs.kt +++ b/js/js.libraries/src/core/generated/_SequencesJs.kt @@ -1401,11 +1401,30 @@ public inline fun Sequence.minusElement(element: T): Sequence { return minus(element) } +/** + * Returns a sequence of pairs of each two adjacent elements in this sequence. + * + * The returned sequence is empty if this sequence contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwise + * + * The operation is _intermediate_ and _stateless_. + */ @SinceKotlin("1.2") public fun Sequence.pairwise(): Sequence> { return pairwise { a, b -> a to b } } +/** + * Returns a sequence containing the results of applying the given [transform] function + * to an each pair of two adjacent elements in this sequence. + * + * The returned sequence is empty if this sequence contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas + * + * The operation is _intermediate_ and _stateless_. + */ @SinceKotlin("1.2") public fun Sequence.pairwise(transform: (a: T, b: T) -> R): Sequence { return buildSequence result@ { diff --git a/js/js.libraries/src/core/generated/_StringsJs.kt b/js/js.libraries/src/core/generated/_StringsJs.kt index db59b1ddd05..12e168f46d5 100644 --- a/js/js.libraries/src/core/generated/_StringsJs.kt +++ b/js/js.libraries/src/core/generated/_StringsJs.kt @@ -1122,11 +1122,26 @@ public fun CharSequence.chunkedSequence(size: Int, transform: (CharSequence) return windowedSequence(size, size, transform) } +/** + * Returns a list of pairs of each two adjacent characters in this char sequence. + * + * The returned list is empty if this char sequence contains less than two characters. + * + * @sample samples.collections.Collections.Transformations.pairwise + */ @SinceKotlin("1.2") public fun CharSequence.pairwise(): List> { return pairwise { a, b -> a to b } } +/** + * Returns a list containing the results of applying the given [transform] function + * to an each pair of two adjacent characters in this char sequence. + * + * The returned list is empty if this char sequence contains less than two characters. + * + * @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas + */ @SinceKotlin("1.2") public inline fun CharSequence.pairwise(transform: (a: Char, b: Char) -> R): List { val size = length - 1 diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index 92a5cad43a6..525a0ee235d 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -930,9 +930,24 @@ public expect operator fun Iterable.minus(elements: Sequence): List @kotlin.internal.InlineOnly public expect inline fun Iterable.minusElement(element: T): List +/** + * Returns a list of pairs of each two adjacent elements in this collection. + * + * The returned list is empty if this collection contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwise + */ @SinceKotlin("1.2") public expect fun Iterable.pairwise(): List> +/** + * Returns a list containing the results of applying the given [transform] function + * to an each pair of two adjacent elements in this collection. + * + * The returned list is empty if this collection contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas + */ @SinceKotlin("1.2") public expect inline fun Iterable.pairwise(transform: (a: T, b: T) -> R): List diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index dab639d5bfa..0edd231fdaa 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -854,9 +854,28 @@ public expect operator fun Sequence.minus(elements: Sequence): Sequenc @kotlin.internal.InlineOnly public expect inline fun Sequence.minusElement(element: T): Sequence +/** + * Returns a sequence of pairs of each two adjacent elements in this sequence. + * + * The returned sequence is empty if this sequence contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwise + * + * The operation is _intermediate_ and _stateless_. + */ @SinceKotlin("1.2") public expect fun Sequence.pairwise(): Sequence> +/** + * Returns a sequence containing the results of applying the given [transform] function + * to an each pair of two adjacent elements in this sequence. + * + * The returned sequence is empty if this sequence contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas + * + * The operation is _intermediate_ and _stateless_. + */ @SinceKotlin("1.2") public expect fun Sequence.pairwise(transform: (a: T, b: T) -> R): Sequence diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index 1fa5fd44998..19c4fab28de 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -650,9 +650,24 @@ public expect fun CharSequence.chunkedSequence(size: Int): Sequence @SinceKotlin("1.2") public expect fun CharSequence.chunkedSequence(size: Int, transform: (CharSequence) -> R): Sequence +/** + * Returns a list of pairs of each two adjacent characters in this char sequence. + * + * The returned list is empty if this char sequence contains less than two characters. + * + * @sample samples.collections.Collections.Transformations.pairwise + */ @SinceKotlin("1.2") public expect fun CharSequence.pairwise(): List> +/** + * Returns a list containing the results of applying the given [transform] function + * to an each pair of two adjacent characters in this char sequence. + * + * The returned list is empty if this char sequence contains less than two characters. + * + * @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas + */ @SinceKotlin("1.2") public expect inline fun CharSequence.pairwise(transform: (a: Char, b: Char) -> R): List diff --git a/libraries/stdlib/samples/test/samples/collections/collections.kt b/libraries/stdlib/samples/test/samples/collections/collections.kt index d26fd140848..f70014fbbfd 100644 --- a/libraries/stdlib/samples/test/samples/collections/collections.kt +++ b/libraries/stdlib/samples/test/samples/collections/collections.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,6 +70,23 @@ class Collections { 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 pairwise() { + val letters = ('a'..'f').toList() + val pairs = letters.pairwise() + + assertPrints(letters, "[a, b, c, d, e, f]") + assertPrints(pairs, "[(a, b), (b, c), (c, d), (d, e), (e, f)]") + } + + @Sample + fun pairwiseToFindDeltas() { + val values = listOf(1, 4, 9, 16, 25, 36) + val deltas = values.pairwise { a, b -> b - a } + + assertPrints(deltas, "[3, 5, 7, 9, 11]") + } } } \ No newline at end of file diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 02231be88fb..0394c517eed 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -1854,11 +1854,26 @@ public inline fun Iterable.minusElement(element: T): List { return minus(element) } +/** + * Returns a list of pairs of each two adjacent elements in this collection. + * + * The returned list is empty if this collection contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwise + */ @SinceKotlin("1.2") public fun Iterable.pairwise(): List> { return pairwise { a, b -> a to b } } +/** + * Returns a list containing the results of applying the given [transform] function + * to an each pair of two adjacent elements in this collection. + * + * The returned list is empty if this collection contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas + */ @SinceKotlin("1.2") public inline fun Iterable.pairwise(transform: (a: T, b: T) -> R): List { val iterator = iterator() diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 077a7e360dc..425f2eabf32 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -1423,11 +1423,30 @@ public inline fun Sequence.minusElement(element: T): Sequence { return minus(element) } +/** + * Returns a sequence of pairs of each two adjacent elements in this sequence. + * + * The returned sequence is empty if this sequence contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwise + * + * The operation is _intermediate_ and _stateless_. + */ @SinceKotlin("1.2") public fun Sequence.pairwise(): Sequence> { return pairwise { a, b -> a to b } } +/** + * Returns a sequence containing the results of applying the given [transform] function + * to an each pair of two adjacent elements in this sequence. + * + * The returned sequence is empty if this sequence contains less than two elements. + * + * @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas + * + * The operation is _intermediate_ and _stateless_. + */ @SinceKotlin("1.2") public fun Sequence.pairwise(transform: (a: T, b: T) -> R): Sequence { return buildSequence result@ { diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index cce2f84ac2c..0ee1a6bb676 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -1130,11 +1130,26 @@ public fun CharSequence.chunkedSequence(size: Int, transform: (CharSequence) return windowedSequence(size, size, transform) } +/** + * Returns a list of pairs of each two adjacent characters in this char sequence. + * + * The returned list is empty if this char sequence contains less than two characters. + * + * @sample samples.collections.Collections.Transformations.pairwise + */ @SinceKotlin("1.2") public fun CharSequence.pairwise(): List> { return pairwise { a, b -> a to b } } +/** + * Returns a list containing the results of applying the given [transform] function + * to an each pair of two adjacent characters in this char sequence. + * + * The returned list is empty if this char sequence contains less than two characters. + * + * @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas + */ @SinceKotlin("1.2") public inline fun CharSequence.pairwise(transform: (a: Char, b: Char) -> R): List { val size = length - 1 diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index 07b7be25390..b72ed48710a 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -718,7 +718,16 @@ fun generators(): List { since("1.2") only(Iterables, Sequences, CharSequences) typeParam("R") + doc { f -> + """ + Returns a ${f.mapResult} containing the results of applying the given [transform] function + to an each pair of two adjacent ${f.element.pluralize()} in this ${f.collection}. + The returned ${f.mapResult} is empty if this ${f.collection} contains less than two ${f.element.pluralize()}. + + @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas + """ + } returns("List") inline(true) body { @@ -748,6 +757,7 @@ fun generators(): List { } inline(false, Sequences) + sequenceClassification(intermediate, stateless) returns(Sequences) { "Sequence" } body(Sequences) { """ @@ -769,6 +779,16 @@ fun generators(): List { since("1.2") only(Iterables, Sequences, CharSequences) returns("List>") + doc { f -> + """ + Returns a ${f.mapResult} of pairs of each two adjacent ${f.element.pluralize()} in this ${f.collection}. + + The returned ${f.mapResult} is empty if this ${f.collection} contains less than two ${f.element.pluralize()}. + + @sample samples.collections.Collections.Transformations.pairwise + """ + } + sequenceClassification(intermediate, stateless) returns(Sequences) { "Sequence>" } body { "return pairwise { a, b -> a to b }"