diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 4e6674f72ae..7a9c4dad824 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -12326,7 +12326,7 @@ public inline fun CharArray.forEach(action: (Char) -> Unit): Unit { /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ public inline fun Array.forEachIndexed(action: (index: Int, T) -> Unit): Unit { var index = 0 @@ -12336,7 +12336,7 @@ public inline fun Array.forEachIndexed(action: (index: Int, T) -> Uni /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ public inline fun ByteArray.forEachIndexed(action: (index: Int, Byte) -> Unit): Unit { var index = 0 @@ -12346,7 +12346,7 @@ public inline fun ByteArray.forEachIndexed(action: (index: Int, Byte) -> Unit): /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ public inline fun ShortArray.forEachIndexed(action: (index: Int, Short) -> Unit): Unit { var index = 0 @@ -12356,7 +12356,7 @@ public inline fun ShortArray.forEachIndexed(action: (index: Int, Short) -> Unit) /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ public inline fun IntArray.forEachIndexed(action: (index: Int, Int) -> Unit): Unit { var index = 0 @@ -12366,7 +12366,7 @@ public inline fun IntArray.forEachIndexed(action: (index: Int, Int) -> Unit): Un /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ public inline fun LongArray.forEachIndexed(action: (index: Int, Long) -> Unit): Unit { var index = 0 @@ -12376,7 +12376,7 @@ public inline fun LongArray.forEachIndexed(action: (index: Int, Long) -> Unit): /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ public inline fun FloatArray.forEachIndexed(action: (index: Int, Float) -> Unit): Unit { var index = 0 @@ -12386,7 +12386,7 @@ public inline fun FloatArray.forEachIndexed(action: (index: Int, Float) -> Unit) /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ public inline fun DoubleArray.forEachIndexed(action: (index: Int, Double) -> Unit): Unit { var index = 0 @@ -12396,7 +12396,7 @@ public inline fun DoubleArray.forEachIndexed(action: (index: Int, Double) -> Uni /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ public inline fun BooleanArray.forEachIndexed(action: (index: Int, Boolean) -> Unit): Unit { var index = 0 @@ -12406,7 +12406,7 @@ public inline fun BooleanArray.forEachIndexed(action: (index: Int, Boolean) -> U /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ public inline fun CharArray.forEachIndexed(action: (index: Int, Char) -> Unit): Unit { var index = 0 @@ -13591,6 +13591,114 @@ public inline fun CharArray.onEach(action: (Char) -> Unit): CharArray { return apply { for (element in this) action(element) } } +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun Array.onEachIndexed(action: (index: Int, T) -> Unit): Array { + return apply { forEachIndexed(action) } +} + +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun ByteArray.onEachIndexed(action: (index: Int, Byte) -> Unit): ByteArray { + return apply { forEachIndexed(action) } +} + +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun ShortArray.onEachIndexed(action: (index: Int, Short) -> Unit): ShortArray { + return apply { forEachIndexed(action) } +} + +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun IntArray.onEachIndexed(action: (index: Int, Int) -> Unit): IntArray { + return apply { forEachIndexed(action) } +} + +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun LongArray.onEachIndexed(action: (index: Int, Long) -> Unit): LongArray { + return apply { forEachIndexed(action) } +} + +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun FloatArray.onEachIndexed(action: (index: Int, Float) -> Unit): FloatArray { + return apply { forEachIndexed(action) } +} + +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun DoubleArray.onEachIndexed(action: (index: Int, Double) -> Unit): DoubleArray { + return apply { forEachIndexed(action) } +} + +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun BooleanArray.onEachIndexed(action: (index: Int, Boolean) -> Unit): BooleanArray { + return apply { forEachIndexed(action) } +} + +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun CharArray.onEachIndexed(action: (index: Int, Char) -> Unit): CharArray { + return apply { forEachIndexed(action) } +} + /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. * diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index 9ff2f45e098..f17f249987c 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -1666,7 +1666,7 @@ public inline fun Iterable.forEach(action: (T) -> Unit): Unit { /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ public inline fun Iterable.forEachIndexed(action: (index: Int, T) -> Unit): Unit { var index = 0 @@ -1878,6 +1878,17 @@ public inline fun > C.onEach(action: (T) -> Unit): C { return apply { for (element in this) action(element) } } +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the collection itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +public inline fun > C.onEachIndexed(action: (index: Int, T) -> Unit): C { + return apply { forEachIndexed(action) } +} + /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. * diff --git a/libraries/stdlib/common/src/generated/_Maps.kt b/libraries/stdlib/common/src/generated/_Maps.kt index dd47821fab7..7408441bff7 100644 --- a/libraries/stdlib/common/src/generated/_Maps.kt +++ b/libraries/stdlib/common/src/generated/_Maps.kt @@ -213,6 +213,17 @@ public inline fun > M.onEach(action: (Map.Entry) - return apply { for (element in this) action(element) } } +/** + * Performs the given [action] on each entry, providing sequential index with the entry, + * and returns the map itself afterwards. + * @param [action] function that takes the index of an entry and the entry itself + * and performs the action on the entry. + */ +@SinceKotlin("1.4") +public inline fun > M.onEachIndexed(action: (index: Int, Map.Entry) -> Unit): M { + return apply { entries.forEachIndexed(action) } +} + /** * Creates an [Iterable] instance that wraps the original map returning its entries when being iterated. */ diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index 03fc73d1bde..2f48c0bf7a1 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -1115,7 +1115,7 @@ public inline fun Sequence.forEach(action: (T) -> Unit): Unit { /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. * * The operation is _terminal_. */ @@ -1356,6 +1356,21 @@ public fun Sequence.onEach(action: (T) -> Unit): Sequence { } } +/** + * Returns a sequence which performs the given [action] on each element of the original sequence as they pass through it. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + * + * The operation is _intermediate_ and _stateless_. + */ +@SinceKotlin("1.4") +public fun Sequence.onEachIndexed(action: (index: Int, T) -> Unit): Sequence { + return mapIndexed { index, element -> + action(index, element) + element + } +} + /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. * diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index f3db92dd03e..7f01993a1f8 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -1065,7 +1065,7 @@ public inline fun CharSequence.forEach(action: (Char) -> Unit): Unit { /** * Performs the given [action] on each character, providing sequential index with the character. * @param [action] function that takes the index of a character and the character itself - * and performs the desired action on the character. + * and performs the action on the character. */ public inline fun CharSequence.forEachIndexed(action: (index: Int, Char) -> Unit): Unit { var index = 0 @@ -1195,6 +1195,17 @@ public inline fun S.onEach(action: (Char) -> Unit): S { return apply { for (element in this) action(element) } } +/** + * Performs the given [action] on each character, providing sequential index with the character, + * and returns the char sequence itself afterwards. + * @param [action] function that takes the index of a character and the character itself + * and performs the action on the character. + */ +@SinceKotlin("1.4") +public inline fun S.onEachIndexed(action: (index: Int, Char) -> Unit): S { + return apply { forEachIndexed(action) } +} + /** * Accumulates value starting with the first character and applying [operation] from left to right to current accumulator value and each character. * diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 80670df9b91..f46db1b3ca4 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -5381,7 +5381,7 @@ public inline fun UShortArray.forEach(action: (UShort) -> Unit): Unit { /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ @SinceKotlin("1.3") @ExperimentalUnsignedTypes @@ -5394,7 +5394,7 @@ public inline fun UIntArray.forEachIndexed(action: (index: Int, UInt) -> Unit): /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ @SinceKotlin("1.3") @ExperimentalUnsignedTypes @@ -5407,7 +5407,7 @@ public inline fun ULongArray.forEachIndexed(action: (index: Int, ULong) -> Unit) /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ @SinceKotlin("1.3") @ExperimentalUnsignedTypes @@ -5420,7 +5420,7 @@ public inline fun UByteArray.forEachIndexed(action: (index: Int, UByte) -> Unit) /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. + * and performs the action on the element. */ @SinceKotlin("1.3") @ExperimentalUnsignedTypes @@ -6010,6 +6010,58 @@ public inline fun UShortArray.onEach(action: (UShort) -> Unit): UShortArray { return apply { for (element in this) action(element) } } +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.onEachIndexed(action: (index: Int, UInt) -> Unit): UIntArray { + return apply { forEachIndexed(action) } +} + +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.onEachIndexed(action: (index: Int, ULong) -> Unit): ULongArray { + return apply { forEachIndexed(action) } +} + +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.onEachIndexed(action: (index: Int, UByte) -> Unit): UByteArray { + return apply { forEachIndexed(action) } +} + +/** + * Performs the given [action] on each element, providing sequential index with the element, + * and returns the array itself afterwards. + * @param [action] function that takes the index of an element and the element itself + * and performs the action on the element. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.onEachIndexed(action: (index: Int, UShort) -> Unit): UShortArray { + return apply { forEachIndexed(action) } +} + /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. * diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index 4994437842b..51450c75faf 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -1441,6 +1441,23 @@ class ArraysTest { assertEquals(listOf('1', '2', '3'), mutableListOf().apply { charArrayOf('1', '2', '3').onEach { add(it) } }) } + @Test fun onEachIndexed() { + assertEquals(listOf(1, 3, 5), mutableListOf().apply { intArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i + e) } }) + assertEquals(listOf(1, 3, 5), mutableListOf().apply { byteArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i + e) } }) + assertEquals(listOf(1, 3, 5), mutableListOf().apply { shortArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i + e) } }) + assertEquals(listOf(1, 3, 5), mutableListOf().apply { longArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i + e) } }) + assertEquals(listOf(1f, 3f, 5f), mutableListOf().apply { floatArrayOf(1f, 2f, 3f).onEachIndexed { i, e -> add(i + e) } }) + assertEquals(listOf(1.0, 3.0, 5.0), mutableListOf().apply { doubleArrayOf(1.0, 2.0, 3.0).onEachIndexed { i, e -> add(i + e) } }) + assertEquals(listOf(true, false, true), mutableListOf().apply { booleanArrayOf(true, false, false).onEachIndexed { i, e -> add(i % 2 == 0 || e) } }) + assertEquals(listOf('1', '3', '5'), mutableListOf().apply { charArrayOf('1', '2', '3').onEachIndexed { i, e -> add(e + i) } }) + assertEquals(listOf("a0", "b1", "c2"), mutableListOf().apply { arrayOf("a", "b", "c").onEachIndexed { i, e -> add(e + i) } }) + + val empty = arrayOf() + assertSame(empty, empty.onEachIndexed { i, e -> fail("Should be unreachable: $i, $e") }) + val nonEmpty = longArrayOf(1, 2, 3) + assertSame(nonEmpty, nonEmpty.onEachIndexed { _, _ -> }) + } + @Test fun drop() { expect(listOf(1), { intArrayOf(1).drop(0) }) expect(listOf(), { intArrayOf().drop(1) }) diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index 9be58dbaac1..ae8c16d2a66 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -313,6 +313,17 @@ abstract class IterableTests>(val createFrom: (Array>(arrayListOf(1, 2, 3).onEach { }) } + @Test + fun onEachIndexed() { + var count = 0 + val newData = data.onEachIndexed { i, e -> count += i + e.length } + assertEquals(7, count) + assertSame(data, newData) + + // static types test + assertStaticTypeIs>(arrayListOf(1, 2, 3).onEachIndexed { _, _ -> }) + } + @Test fun contains() { assertTrue(data.contains("foo")) diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index 45d866ac2a9..bbc62e9354d 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -128,6 +128,20 @@ class MapTest { ) } + @Test + fun onEachIndexed() { + val map = mutableMapOf("beverage" to "beer", "location" to "Mells") + val result = StringBuilder() + val newMap = map.onEachIndexed { i, e -> result.append(i + 1).append('.').append(e.key).append("=").append(e.value).append(";") } + assertEquals("1.beverage=beer;2.location=Mells;", result.toString()) + assertTrue(map === newMap) + + // static types test + assertStaticTypeIs>( + hashMapOf("a" to "b").onEachIndexed { _, _ -> } + ) + } + @Test fun stream() { val map = mapOf("beverage" to "beer", "location" to "Mells", "name" to "James") val named = map.asSequence().filter { it.key == "name" }.single() diff --git a/libraries/stdlib/test/collections/SequenceTest.kt b/libraries/stdlib/test/collections/SequenceTest.kt index 67373311aba..824a92386d7 100644 --- a/libraries/stdlib/test/collections/SequenceTest.kt +++ b/libraries/stdlib/test/collections/SequenceTest.kt @@ -126,6 +126,21 @@ public class SequenceTest { assertEquals(sum, count) } + @Test + fun onEachIndexed() { + var count = 0 + val data = sequenceOf("foo", "bar") + val newData = data.onEachIndexed { i, e -> count += i + e.length } + assertNotSame(data, newData) + assertEquals(0, count, "onEachIndex should be executed lazily") + + data.forEach { } + assertEquals(0, count, "onEachIndex should be executed only when resulting sequence is iterated") + + val sum = newData.foldIndexed(0) { i, acc, e -> acc + i + e.length } + assertEquals(sum, count) + } + @Test fun filterAndTakeWhileExtractTheElementsWithinRange() { assertEquals(listOf(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList()) diff --git a/libraries/stdlib/test/collections/UnsignedArraysTest.kt b/libraries/stdlib/test/collections/UnsignedArraysTest.kt index a94f1894fb2..892f904e216 100644 --- a/libraries/stdlib/test/collections/UnsignedArraysTest.kt +++ b/libraries/stdlib/test/collections/UnsignedArraysTest.kt @@ -994,6 +994,21 @@ class UnsignedArraysTest { assertEquals(listOf(1, 2, 3), mutableListOf().apply { ulongArrayOf(1, 2, 3).onEach { add(it) } }) } + @Test + fun onEachIndexed() { + assertEquals(listOf(1, 3, 5), mutableListOf().apply { uintArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toUInt() + e) } }) + assertEquals(listOf(1, 3, 5), mutableListOf().apply { ubyteArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toUByte() + e) } }) + assertEquals(listOf(1, 3, 5), mutableListOf().apply { ushortArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toUShort() + e) } }) + assertEquals(listOf(1, 3, 5), mutableListOf().apply { ulongArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toULong() + e) } }) + + val empty = arrayOf() + assertSame(empty, empty.onEachIndexed { i, e -> fail("Should be unreachable: $i, $e") }) + + // Identity equality for arguments of types ULongArray and ULongArray is forbidden +// val nonEmpty = ulongArrayOf(1, 2, 3) +// assertSame(nonEmpty, nonEmpty.onEachIndexed { _, _ -> }) + } + @Test fun drop() { expect(listOf(1.toUByte())) { ubyteArrayOf(1).drop(0) } diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 957b6fb3f81..9ba2179ac71 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -948,6 +948,18 @@ class StringTest { assertStaticTypeIs(result.onEach { }) } + @Test + fun onEachIndexed() = withOneCharSequenceArg("abcd") { data -> + val result = StringBuilder() + val newData = data.onEachIndexed { i, e -> result.append(e + i) } + assertEquals("aceg", result.toString()) + assertSame(data, newData) + + // static types test + assertStaticTypeIs("x".onEachIndexed { _, _ -> }) + assertStaticTypeIs(result.onEachIndexed { _, _ -> }) + } + @Test fun filter() { assertEquals("acdca", ("abcdcba").filter { !it.equals('b') }) 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 63d8cd63182..06ad803c221 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 @@ -2152,6 +2152,7 @@ public final class kotlin/collections/CollectionsKt { public static final fun none (Ljava/lang/Iterable;)Z public static final fun none (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Z public static final fun onEach (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Ljava/lang/Iterable; + public static final fun onEachIndexed (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/lang/Iterable; public static final fun partition (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Lkotlin/Pair; public static final fun plus (Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/util/List; public static final fun plus (Ljava/lang/Iterable;Ljava/lang/Object;)Ljava/util/List; @@ -2369,6 +2370,7 @@ public final class kotlin/collections/MapsKt { public static final fun none (Ljava/util/Map;)Z public static final fun none (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Z public static final fun onEach (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map; + public static final fun onEachIndexed (Ljava/util/Map;Lkotlin/jvm/functions/Function2;)Ljava/util/Map; public static final fun plus (Ljava/util/Map;Ljava/lang/Iterable;)Ljava/util/Map; public static final fun plus (Ljava/util/Map;Ljava/util/Map;)Ljava/util/Map; public static final fun plus (Ljava/util/Map;Lkotlin/Pair;)Ljava/util/Map; @@ -4724,6 +4726,7 @@ public final class kotlin/sequences/SequencesKt { public static final fun none (Lkotlin/sequences/Sequence;)Z public static final fun none (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Z public static final fun onEach (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence; + public static final fun onEachIndexed (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence; public static final fun partition (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/Pair; public static final fun plus (Lkotlin/sequences/Sequence;Ljava/lang/Iterable;)Lkotlin/sequences/Sequence; public static final fun plus (Lkotlin/sequences/Sequence;Ljava/lang/Object;)Lkotlin/sequences/Sequence; @@ -5116,6 +5119,7 @@ public final class kotlin/text/StringsKt { public static final fun none (Ljava/lang/CharSequence;)Z public static final fun none (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Z public static final fun onEach (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/CharSequence; + public static final fun onEachIndexed (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function2;)Ljava/lang/CharSequence; public static final fun padEnd (Ljava/lang/CharSequence;IC)Ljava/lang/CharSequence; public static final fun padEnd (Ljava/lang/String;IC)Ljava/lang/String; public static synthetic fun padEnd$default (Ljava/lang/CharSequence;ICILjava/lang/Object;)Ljava/lang/CharSequence; diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index 8d00d2b7e15..b24bdbf04c6 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -1609,6 +1609,61 @@ object Aggregates : TemplateGroupBase() { } } + val f_onEachIndexed = fn("onEachIndexed(action: (index: Int, T) -> Unit)") { + includeDefault() + include(Maps, CharSequences, ArraysOfUnsigned) + } builder { + since("1.4") + + doc { + """ + Performs the given [action] on each ${f.element}, providing sequential index with the ${f.element}, + and returns the ${f.collection} itself afterwards. + @param [action] function that takes the index of ${f.element.prefixWithArticle()} and the ${f.element} itself + and performs the action on the ${f.element}. + """ + } + + specialFor(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { + inlineOnly() + returns("SELF") + body { "return apply { forEachIndexed(action) }" } + } + + specialFor(Maps, Iterables, CharSequences) { + inline() + val collectionType = when (f) { + Maps -> "M" + CharSequences -> "S" + else -> "C" + } + receiver(collectionType) + returns(collectionType) + typeParam("$collectionType : SELF") + body { "return apply { ${if (f == Maps) "entries." else ""}forEachIndexed(action) }" } + } + + specialFor(Sequences) { + returns("SELF") + doc { + """ + Returns a sequence which performs the given [action] on each ${f.element} of the original sequence as they pass through it. + @param [action] function that takes the index of ${f.element.prefixWithArticle()} and the ${f.element} itself + and performs the action on the ${f.element}. + """ + } + sequenceClassification(intermediate, stateless) + body { + """ + return mapIndexed { index, element -> + action(index, element) + element + } + """ + } + } + } + val f_forEach = fn("forEach(action: (T) -> Unit)") { includeDefault() include(Maps, CharSequences, ArraysOfUnsigned) @@ -1637,7 +1692,7 @@ object Aggregates : TemplateGroupBase() { """ Performs the given [action] on each ${f.element}, providing sequential index with the ${f.element}. @param [action] function that takes the index of ${f.element.prefixWithArticle()} and the ${f.element} itself - and performs the desired action on the ${f.element}. + and performs the action on the ${f.element}. """ } returns("Unit") body {