diff --git a/js/js.libraries/src/core/generated/_CollectionsJs.kt b/js/js.libraries/src/core/generated/_CollectionsJs.kt index 4d88f2887af..b291545a76e 100644 --- a/js/js.libraries/src/core/generated/_CollectionsJs.kt +++ b/js/js.libraries/src/core/generated/_CollectionsJs.kt @@ -1867,38 +1867,16 @@ 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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()")) @SinceKotlin("1.2") public fun Iterable.pairwise(): List> { - return pairwise { a, b -> a to b } + return zipWithNext { 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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)")) @SinceKotlin("1.2") public inline fun Iterable.pairwise(transform: (a: T, b: T) -> R): List { - val iterator = iterator() - if (!iterator.hasNext()) return emptyList() - val result = mutableListOf() - var current = iterator.next() - while (iterator.hasNext()) { - val next = iterator.next() - result.add(transform(current, next)) - current = next - } - return result + return zipWithNext(transform) } /** @@ -2135,6 +2113,40 @@ public inline fun Iterable.zip(other: Iterable, transform: (a: T return 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.zipWithNext + */ +@SinceKotlin("1.2") +public fun Iterable.zipWithNext(): List> { + return zipWithNext { 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.zipWithNextToFindDeltas + */ +@SinceKotlin("1.2") +public inline fun Iterable.zipWithNext(transform: (a: T, b: T) -> R): List { + val iterator = iterator() + if (!iterator.hasNext()) return emptyList() + val result = mutableListOf() + var current = iterator.next() + while (iterator.hasNext()) { + val next = iterator.next() + result.add(transform(current, next)) + current = next + } + return result +} + /** * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. * diff --git a/js/js.libraries/src/core/generated/_SequencesJs.kt b/js/js.libraries/src/core/generated/_SequencesJs.kt index 8d4d8d09b4e..aa2edb099c7 100644 --- a/js/js.libraries/src/core/generated/_SequencesJs.kt +++ b/js/js.libraries/src/core/generated/_SequencesJs.kt @@ -1428,42 +1428,16 @@ 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_. - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()")) @SinceKotlin("1.2") public fun Sequence.pairwise(): Sequence> { - return pairwise { a, b -> a to b } + return zipWithNext { 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_. - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)")) @SinceKotlin("1.2") public fun Sequence.pairwise(transform: (a: T, b: T) -> R): Sequence { - return buildSequence result@ { - val iterator = iterator() - if (!iterator.hasNext()) return@result - var current = iterator.next() - while (iterator.hasNext()) { - val next = iterator.next() - yield(transform(current, next)) - current = next - } - } + return zipWithNext(transform) } /** @@ -1598,6 +1572,44 @@ public fun Sequence.zip(other: Sequence, transform: (a: T, b: R) return MergingSequence(this, other, transform) } +/** + * 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.zipWithNext + * + * The operation is _intermediate_ and _stateless_. + */ +@SinceKotlin("1.2") +public fun Sequence.zipWithNext(): Sequence> { + return zipWithNext { 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.zipWithNextToFindDeltas + * + * The operation is _intermediate_ and _stateless_. + */ +@SinceKotlin("1.2") +public fun Sequence.zipWithNext(transform: (a: T, b: T) -> R): Sequence { + return buildSequence result@ { + val iterator = iterator() + if (!iterator.hasNext()) return@result + var current = iterator.next() + while (iterator.hasNext()) { + val next = iterator.next() + yield(transform(current, next)) + current = next + } + } +} + /** * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. * diff --git a/js/js.libraries/src/core/generated/_StringsJs.kt b/js/js.libraries/src/core/generated/_StringsJs.kt index 11642c285e9..7e241ef91dc 100644 --- a/js/js.libraries/src/core/generated/_StringsJs.kt +++ b/js/js.libraries/src/core/generated/_StringsJs.kt @@ -1168,35 +1168,16 @@ 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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()")) @SinceKotlin("1.2") public fun CharSequence.pairwise(): List> { - return pairwise { a, b -> a to b } + return zipWithNext { 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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)")) @SinceKotlin("1.2") public inline fun CharSequence.pairwise(transform: (a: Char, b: Char) -> R): List { - val size = length - 1 - if (size < 1) return emptyList() - val result = ArrayList(size) - for (index in 0..size - 1) { - result.add(transform(this[index], this[index + 1])) - } - return result + return zipWithNext(transform) } /** @@ -1339,6 +1320,37 @@ public inline fun CharSequence.zip(other: CharSequence, transform: (a: Char, return list } +/** + * 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.zipWithNext + */ +@SinceKotlin("1.2") +public fun CharSequence.zipWithNext(): List> { + return zipWithNext { 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.zipWithNextToFindDeltas + */ +@SinceKotlin("1.2") +public inline fun CharSequence.zipWithNext(transform: (a: Char, b: Char) -> R): List { + val size = length - 1 + if (size < 1) return emptyList() + val result = ArrayList(size) + for (index in 0..size - 1) { + result.add(transform(this[index], this[index + 1])) + } + return result +} + /** * Creates an [Iterable] instance that wraps the original char sequence returning its characters when being iterated. */ diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index e615ec9ebb0..622468532b3 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -953,24 +953,11 @@ 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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()")) @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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)")) @SinceKotlin("1.2") public expect inline fun Iterable.pairwise(transform: (a: T, b: T) -> R): List @@ -1087,6 +1074,27 @@ public expect infix fun Iterable.zip(other: Iterable): List Iterable.zip(other: Iterable, transform: (a: T, b: R) -> V): 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.zipWithNext + */ +@SinceKotlin("1.2") +public expect fun Iterable.zipWithNext(): 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.zipWithNextToFindDeltas + */ +@SinceKotlin("1.2") +public expect inline fun Iterable.zipWithNext(transform: (a: T, b: T) -> R): List + /** * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. * diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index 2a86ec73328..5125f7fa3fc 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -881,28 +881,11 @@ 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_. - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()")) @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_. - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)")) @SinceKotlin("1.2") public expect fun Sequence.pairwise(transform: (a: T, b: T) -> R): Sequence @@ -1009,6 +992,31 @@ public expect infix fun Sequence.zip(other: Sequence): Sequence Sequence.zip(other: Sequence, transform: (a: T, b: R) -> V): 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.zipWithNext + * + * The operation is _intermediate_ and _stateless_. + */ +@SinceKotlin("1.2") +public expect fun Sequence.zipWithNext(): 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.zipWithNextToFindDeltas + * + * The operation is _intermediate_ and _stateless_. + */ +@SinceKotlin("1.2") +public expect fun Sequence.zipWithNext(transform: (a: T, b: T) -> R): Sequence + /** * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. * diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index 48fc41ba177..5f9e5d4e8c4 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -696,24 +696,11 @@ 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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()")) @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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)")) @SinceKotlin("1.2") public expect inline fun CharSequence.pairwise(transform: (a: Char, b: Char) -> R): List @@ -809,6 +796,27 @@ public expect infix fun CharSequence.zip(other: CharSequence): List CharSequence.zip(other: CharSequence, transform: (a: Char, b: Char) -> V): List +/** + * 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.zipWithNext + */ +@SinceKotlin("1.2") +public expect fun CharSequence.zipWithNext(): 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.zipWithNextToFindDeltas + */ +@SinceKotlin("1.2") +public expect inline fun CharSequence.zipWithNext(transform: (a: Char, b: Char) -> R): List + /** * Creates an [Iterable] instance that wraps the original char sequence returning its characters when being iterated. */ diff --git a/libraries/stdlib/samples/test/samples/collections/collections.kt b/libraries/stdlib/samples/test/samples/collections/collections.kt index 57e1112d629..de680035426 100644 --- a/libraries/stdlib/samples/test/samples/collections/collections.kt +++ b/libraries/stdlib/samples/test/samples/collections/collections.kt @@ -81,18 +81,18 @@ class Collections { @Sample - fun pairwise() { + fun zipWithNext() { val letters = ('a'..'f').toList() - val pairs = letters.pairwise() + val pairs = letters.zipWithNext() assertPrints(letters, "[a, b, c, d, e, f]") assertPrints(pairs, "[(a, b), (b, c), (c, d), (d, e), (e, f)]") } @Sample - fun pairwiseToFindDeltas() { + fun zipWithNextToFindDeltas() { val values = listOf(1, 4, 9, 16, 25, 36) - val deltas = values.pairwise { a, b -> b - a } + val deltas = values.zipWithNext { a, b -> b - a } assertPrints(deltas, "[3, 5, 7, 9, 11]") } diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 7ef537aae3d..02a77b8b1b7 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -1877,38 +1877,16 @@ 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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()")) @SinceKotlin("1.2") public fun Iterable.pairwise(): List> { - return pairwise { a, b -> a to b } + return zipWithNext { 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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)")) @SinceKotlin("1.2") public inline fun Iterable.pairwise(transform: (a: T, b: T) -> R): List { - val iterator = iterator() - if (!iterator.hasNext()) return emptyList() - val result = mutableListOf() - var current = iterator.next() - while (iterator.hasNext()) { - val next = iterator.next() - result.add(transform(current, next)) - current = next - } - return result + return zipWithNext(transform) } /** @@ -2145,6 +2123,40 @@ public inline fun Iterable.zip(other: Iterable, transform: (a: T return 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.zipWithNext + */ +@SinceKotlin("1.2") +public fun Iterable.zipWithNext(): List> { + return zipWithNext { 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.zipWithNextToFindDeltas + */ +@SinceKotlin("1.2") +public inline fun Iterable.zipWithNext(transform: (a: T, b: T) -> R): List { + val iterator = iterator() + if (!iterator.hasNext()) return emptyList() + val result = mutableListOf() + var current = iterator.next() + while (iterator.hasNext()) { + val next = iterator.next() + result.add(transform(current, next)) + current = next + } + return result +} + /** * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. * diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 20ffba860bc..e0828c8130d 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -1450,42 +1450,16 @@ 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_. - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()")) @SinceKotlin("1.2") public fun Sequence.pairwise(): Sequence> { - return pairwise { a, b -> a to b } + return zipWithNext { 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_. - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)")) @SinceKotlin("1.2") public fun Sequence.pairwise(transform: (a: T, b: T) -> R): Sequence { - return buildSequence result@ { - val iterator = iterator() - if (!iterator.hasNext()) return@result - var current = iterator.next() - while (iterator.hasNext()) { - val next = iterator.next() - yield(transform(current, next)) - current = next - } - } + return zipWithNext(transform) } /** @@ -1620,6 +1594,44 @@ public fun Sequence.zip(other: Sequence, transform: (a: T, b: R) return MergingSequence(this, other, transform) } +/** + * 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.zipWithNext + * + * The operation is _intermediate_ and _stateless_. + */ +@SinceKotlin("1.2") +public fun Sequence.zipWithNext(): Sequence> { + return zipWithNext { 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.zipWithNextToFindDeltas + * + * The operation is _intermediate_ and _stateless_. + */ +@SinceKotlin("1.2") +public fun Sequence.zipWithNext(transform: (a: T, b: T) -> R): Sequence { + return buildSequence result@ { + val iterator = iterator() + if (!iterator.hasNext()) return@result + var current = iterator.next() + while (iterator.hasNext()) { + val next = iterator.next() + yield(transform(current, next)) + current = next + } + } +} + /** * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. * diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index 6a3b512f93d..5b713908a61 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -1176,35 +1176,16 @@ 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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()")) @SinceKotlin("1.2") public fun CharSequence.pairwise(): List> { - return pairwise { a, b -> a to b } + return zipWithNext { 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 - */ +@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)")) @SinceKotlin("1.2") public inline fun CharSequence.pairwise(transform: (a: Char, b: Char) -> R): List { - val size = length - 1 - if (size < 1) return emptyList() - val result = ArrayList(size) - for (index in 0..size - 1) { - result.add(transform(this[index], this[index + 1])) - } - return result + return zipWithNext(transform) } /** @@ -1347,6 +1328,37 @@ public inline fun CharSequence.zip(other: CharSequence, transform: (a: Char, return list } +/** + * 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.zipWithNext + */ +@SinceKotlin("1.2") +public fun CharSequence.zipWithNext(): List> { + return zipWithNext { 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.zipWithNextToFindDeltas + */ +@SinceKotlin("1.2") +public inline fun CharSequence.zipWithNext(transform: (a: Char, b: Char) -> R): List { + val size = length - 1 + if (size < 1) return emptyList() + val result = ArrayList(size) + for (index in 0..size - 1) { + result.add(transform(this[index], this[index + 1])) + } + return result +} + /** * Creates an [Iterable] instance that wraps the original char sequence returning its characters when being iterated. */ diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index 68a2a338c2e..7c090a06761 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -119,21 +119,21 @@ abstract class OrderedIterableTests>(createFrom: (Array b.length - a.length } + val lengthDeltas = data.zipWithNext { a: String, b: String -> b.length - a.length } assertEquals(listOf(1, 2), lengthDeltas) - assertTrue(empty.pairwise { a, b -> a + b }.isEmpty()) - assertTrue(createFrom("foo").pairwise { a, b -> a + b }.isEmpty()) + assertTrue(empty.zipWithNext { a: String, b: String -> a + b }.isEmpty()) + assertTrue(createFrom("foo").zipWithNext { a: String, b: String -> a + b }.isEmpty()) } @Test - fun pairwisePairs() { - assertTrue(empty.pairwise().isEmpty()) - assertTrue(createFrom("foo").pairwise().isEmpty()) - assertEquals(listOf("a" to "b"), createFrom("a", "b").pairwise()) - assertEquals(listOf("a" to "b", "b" to "c"), createFrom("a", "b", "c").pairwise()) + fun zipWithNextPairs() { + assertTrue(empty.zipWithNext().isEmpty()) + assertTrue(createFrom("foo").zipWithNext().isEmpty()) + assertEquals(listOf("a" to "b"), createFrom("a", "b").zipWithNext()) + assertEquals(listOf("a" to "b", "b" to "c"), createFrom("a", "b", "c").zipWithNext()) } @Test diff --git a/libraries/stdlib/test/collections/SequenceTest.kt b/libraries/stdlib/test/collections/SequenceTest.kt index 7860d64dcbd..5770c0fa0f0 100644 --- a/libraries/stdlib/test/collections/SequenceTest.kt +++ b/libraries/stdlib/test/collections/SequenceTest.kt @@ -197,23 +197,23 @@ public class SequenceTest { assertEquals("", sequenceOf(1).dropWhile { it < 200 }.joinToString(limit = 10)) } - @Test fun pairwise() { - val deltas = fibonacci().pairwise { a, b -> b - a } + @Test fun zipWithNext() { + val deltas = fibonacci().zipWithNext { a: Int, b: Int -> b - a } // deltas of 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ... // is the same sequence prepended by 1 assertEquals(listOf(1) + fibonacci().take(9), deltas.take(10).toList()) - ensureIsIntermediate(source = sequenceOf(1, 2)) { it.pairwise { a, b -> b - a } } + ensureIsIntermediate(source = sequenceOf(1, 2)) { it.zipWithNext { a: Int, b: Int -> b - a } } } - @Test fun pairwisePairs() { - val pairs: Sequence> = sequenceOf("a", "b", "c", "d").pairwise() + @Test fun zipWithNextPairs() { + val pairs: Sequence> = sequenceOf("a", "b", "c", "d").zipWithNext() assertEquals(listOf("a" to "b", "b" to "c", "c" to "d"), pairs.toList()) - assertTrue(emptySequence().pairwise().toList().isEmpty()) - assertTrue(sequenceOf(1).pairwise().toList().isEmpty()) + assertTrue(emptySequence().zipWithNext().toList().isEmpty()) + assertTrue(sequenceOf(1).zipWithNext().toList().isEmpty()) - ensureIsIntermediate(source = sequenceOf(1, 2)) { it.pairwise() } + ensureIsIntermediate(source = sequenceOf(1, 2)) { it.zipWithNext() } } @Test diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 83000861eeb..dfaa190a77c 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -839,16 +839,16 @@ class StringTest { assertContentEquals("abc", pair.second, "pair.second") } - @Test fun pairwise() = withOneCharSequenceArg { arg1 -> - assertEquals(listOf("ab", "bc"), arg1("abc").pairwise { a, b -> a.toString() + b }) - assertTrue(arg1("").pairwise { a, b -> a.toString() + b }.isEmpty()) - assertTrue(arg1("a").pairwise { a, b -> a.toString() + b }.isEmpty()) + @Test fun zipWithNext() = withOneCharSequenceArg { arg1 -> + assertEquals(listOf("ab", "bc"), arg1("abc").zipWithNext { a: Char, b: Char -> a.toString() + b }) + assertTrue(arg1("").zipWithNext { a: Char, b: Char -> a.toString() + b }.isEmpty()) + assertTrue(arg1("a").zipWithNext { a: Char, b: Char -> a.toString() + b }.isEmpty()) } - @Test fun pairwisePairs() = withOneCharSequenceArg { arg1 -> - assertEquals(listOf('a' to 'b', 'b' to 'c'), arg1("abc").pairwise()) - assertTrue(arg1("").pairwise().isEmpty()) - assertTrue(arg1("a").pairwise().isEmpty()) + @Test fun zipWithNextPairs() = withOneCharSequenceArg { arg1 -> + assertEquals(listOf('a' to 'b', 'b' to 'c'), arg1("abc").zipWithNext()) + assertTrue(arg1("").zipWithNext().isEmpty()) + assertTrue(arg1("a").zipWithNext().isEmpty()) } diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index 72453d69388..28fc6d56279 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -1757,6 +1757,8 @@ public final class kotlin/collections/CollectionsKt { public static final fun zip (Ljava/lang/Iterable;Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/util/List; public static final fun zip (Ljava/lang/Iterable;[Ljava/lang/Object;)Ljava/util/List; public static final fun zip (Ljava/lang/Iterable;[Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/util/List; + public static final fun zipWithNext (Ljava/lang/Iterable;)Ljava/util/List; + public static final fun zipWithNext (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/util/List; } public abstract class kotlin/collections/DoubleIterator : java/util/Iterator, kotlin/jvm/internal/markers/KMappedMarker { @@ -3605,6 +3607,8 @@ public final class kotlin/sequences/SequencesKt { public static final fun withIndex (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence; public static final fun zip (Lkotlin/sequences/Sequence;Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence; public static final fun zip (Lkotlin/sequences/Sequence;Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence; + public static final fun zipWithNext (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence; + public static final fun zipWithNext (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence; } public final class kotlin/system/TimingKt { @@ -4085,6 +4089,8 @@ public final class kotlin/text/StringsKt { public static final fun withIndex (Ljava/lang/CharSequence;)Ljava/lang/Iterable; public static final fun zip (Ljava/lang/CharSequence;Ljava/lang/CharSequence;)Ljava/util/List; public static final fun zip (Ljava/lang/CharSequence;Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function2;)Ljava/util/List; + public static final fun zipWithNext (Ljava/lang/CharSequence;)Ljava/util/List; + public static final fun zipWithNext (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function2;)Ljava/util/List; } public final class kotlin/text/Typography { diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt index 35ad4cb64e1..a9a12889c13 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt @@ -1616,6 +1616,8 @@ public final class kotlin/collections/CollectionsKt { public static final fun zip (Ljava/lang/Iterable;Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/util/List; public static final fun zip (Ljava/lang/Iterable;[Ljava/lang/Object;)Ljava/util/List; public static final fun zip (Ljava/lang/Iterable;[Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/util/List; + public static final fun zipWithNext (Ljava/lang/Iterable;)Ljava/util/List; + public static final fun zipWithNext (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/util/List; } public abstract interface class kotlin/collections/Grouping { @@ -2300,6 +2302,8 @@ public final class kotlin/sequences/SequencesKt { public static final fun withIndex (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence; public static final fun zip (Lkotlin/sequences/Sequence;Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence; public static final fun zip (Lkotlin/sequences/Sequence;Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence; + public static final fun zipWithNext (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence; + public static final fun zipWithNext (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence; } public final class kotlin/system/TimingKt { @@ -2780,6 +2784,8 @@ public final class kotlin/text/StringsKt { public static final fun withIndex (Ljava/lang/CharSequence;)Ljava/lang/Iterable; public static final fun zip (Ljava/lang/CharSequence;Ljava/lang/CharSequence;)Ljava/util/List; public static final fun zip (Ljava/lang/CharSequence;Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function2;)Ljava/util/List; + public static final fun zipWithNext (Ljava/lang/CharSequence;)Ljava/util/List; + public static final fun zipWithNext (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function2;)Ljava/util/List; } public final class kotlin/text/Typography { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index c19aab71eb9..fa66f3af879 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -837,6 +837,22 @@ fun generators(): List { } templates add f("pairwise(transform: (a: T, b: T) -> R)") { + deprecate(Deprecation("Use zipWithNext instead", "zipWithNext(transform)")) + since("1.2") + only(Iterables, Sequences, CharSequences) + typeParam("R") + returns("List") + inline(true) + body { + """ + return zipWithNext(transform) + """ + } + inline(false, Sequences) + returns(Sequences) { "Sequence" } + } + + templates add f("zipWithNext(transform: (a: T, b: T) -> R)") { since("1.2") only(Iterables, Sequences, CharSequences) typeParam("R") @@ -847,7 +863,7 @@ fun generators(): List { The returned ${f.mapResult} is empty if this ${f.collection} contains less than two ${f.element.pluralize()}. - @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas + @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas """ } returns("List") @@ -898,6 +914,17 @@ fun generators(): List { } templates add f("pairwise()") { + deprecate(Deprecation("Use zipWithNext instead", "zipWithNext()")) + since("1.2") + only(Iterables, Sequences, CharSequences) + returns("List>") + returns(Sequences) { "Sequence>" } + body { + "return zipWithNext { a, b -> a to b }" + } + } + + templates add f("zipWithNext()") { since("1.2") only(Iterables, Sequences, CharSequences) returns("List>") @@ -907,13 +934,13 @@ fun generators(): List { The returned ${f.mapResult} is empty if this ${f.collection} contains less than two ${f.element.pluralize()}. - @sample samples.collections.Collections.Transformations.pairwise + @sample samples.collections.Collections.Transformations.zipWithNext """ } sequenceClassification(intermediate, stateless) returns(Sequences) { "Sequence>" } body { - "return pairwise { a, b -> a to b }" + "return zipWithNext { a, b -> a to b }" } }