diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 035a2eabe42..6da164b0f28 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -13303,6 +13303,87 @@ public inline fun CharArray.none(predicate: (Char) -> Boolean): Boolean { return true } +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun Array.onEach(action: (T) -> Unit): Array { + return apply { for (element in this) action(element) } +} + +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun ByteArray.onEach(action: (Byte) -> Unit): ByteArray { + return apply { for (element in this) action(element) } +} + +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun ShortArray.onEach(action: (Short) -> Unit): ShortArray { + return apply { for (element in this) action(element) } +} + +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun IntArray.onEach(action: (Int) -> Unit): IntArray { + return apply { for (element in this) action(element) } +} + +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun LongArray.onEach(action: (Long) -> Unit): LongArray { + return apply { for (element in this) action(element) } +} + +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun FloatArray.onEach(action: (Float) -> Unit): FloatArray { + return apply { for (element in this) action(element) } +} + +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun DoubleArray.onEach(action: (Double) -> Unit): DoubleArray { + return apply { for (element in this) action(element) } +} + +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun BooleanArray.onEach(action: (Boolean) -> Unit): BooleanArray { + return apply { for (element in this) action(element) } +} + +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun CharArray.onEach(action: (Char) -> Unit): CharArray { + return apply { for (element in this) action(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/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 8936023e98e..566f443ada2 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -5870,6 +5870,46 @@ public inline fun UShortArray.none(predicate: (UShort) -> Boolean): Boolean { return true } +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.onEach(action: (UInt) -> Unit): UIntArray { + return apply { for (element in this) action(element) } +} + +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.onEach(action: (ULong) -> Unit): ULongArray { + return apply { for (element in this) action(element) } +} + +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.onEach(action: (UByte) -> Unit): UByteArray { + return apply { for (element in this) action(element) } +} + +/** + * Performs the given [action] on each element and returns the array itself afterwards. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.onEach(action: (UShort) -> Unit): UShortArray { + return apply { for (element in this) action(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/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index 248984e9590..dad4b1cdaa8 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -1416,6 +1416,31 @@ class ArraysTest { assertArrayNotSameButEquals(arrayOf("3", "2", "1"), (arrayOf("1", "2", "3") as Array).reversedArray()) } + @Test fun onEach() { + var count = 0 + val data = intArrayOf(1, 2, 3) + val newData = data.onEach { count += it } + assertEquals(6, count) + assertSame(newData, data) + + var concat = "" + val strings = arrayOf("a", "b", "c") + val newStrings = strings.onEach { concat += it } + assertEquals("abc", concat) + assertSame(newStrings, strings) + + assertEquals(listOf("a", "b", "c"), mutableListOf().apply { arrayOf("a", "b", "c").onEach { add(it) } }) + + assertEquals(listOf(1, 2, 3), mutableListOf().apply { intArrayOf(1, 2, 3).onEach { add(it) } }) + assertEquals(listOf(1, 2, 3), mutableListOf().apply { byteArrayOf(1, 2, 3).onEach { add(it) } }) + assertEquals(listOf(1, 2, 3), mutableListOf().apply { shortArrayOf(1, 2, 3).onEach { add(it) } }) + assertEquals(listOf(1, 2, 3), mutableListOf().apply { longArrayOf(1, 2, 3).onEach { add(it) } }) + assertEquals(listOf(1f, 2f, 3f), mutableListOf().apply { floatArrayOf(1f, 2f, 3f).onEach { add(it) } }) + assertEquals(listOf(1.0, 2.0, 3.0), mutableListOf().apply { doubleArrayOf(1.0, 2.0, 3.0).onEach { add(it) } }) + assertEquals(listOf(true, false, false), mutableListOf().apply { booleanArrayOf(true, false, false).onEach { add(it) } }) + assertEquals(listOf('1', '2', '3'), mutableListOf().apply { charArrayOf('1', '2', '3').onEach { add(it) } }) + } + @Test fun drop() { expect(listOf(1), { intArrayOf(1).drop(0) }) expect(listOf(), { intArrayOf().drop(1) }) diff --git a/libraries/stdlib/test/collections/UnsignedArraysTest.kt b/libraries/stdlib/test/collections/UnsignedArraysTest.kt index 750ada1279b..a94f1894fb2 100644 --- a/libraries/stdlib/test/collections/UnsignedArraysTest.kt +++ b/libraries/stdlib/test/collections/UnsignedArraysTest.kt @@ -986,6 +986,14 @@ class UnsignedArraysTest { ) } + @Test + fun onEach() { + assertEquals(listOf(1, 2, 3), mutableListOf().apply { uintArrayOf(1, 2, 3).onEach { add(it) } }) + assertEquals(listOf(1, 2, 3), mutableListOf().apply { ubyteArrayOf(1, 2, 3).onEach { add(it) } }) + assertEquals(listOf(1, 2, 3), mutableListOf().apply { ushortArrayOf(1, 2, 3).onEach { add(it) } }) + assertEquals(listOf(1, 2, 3), mutableListOf().apply { ulongArrayOf(1, 2, 3).onEach { add(it) } }) + } + @Test fun drop() { expect(listOf(1.toUByte())) { ubyteArrayOf(1).drop(0) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index 92597b97e74..8d00d2b7e15 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -1567,13 +1567,21 @@ object Aggregates : TemplateGroupBase() { } val f_onEach = fn("onEach(action: (T) -> Unit)") { - include(Iterables, Maps, CharSequences, Sequences) + includeDefault() + include(Maps, CharSequences, ArraysOfUnsigned) } builder { since("1.1") + doc { "Performs the given [action] on each ${f.element} and returns the ${f.collection} itself afterwards." } + + specialFor(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { + since("1.4") + inlineOnly() + returns("SELF") + body { "return apply { for (element in this) action(element) }" } + } specialFor(Iterables, Maps, CharSequences) { inline() - doc { "Performs the given [action] on each ${f.element} and returns the ${f.collection} itself afterwards." } val collectionType = when (f) { Maps -> "M" CharSequences -> "S"