Add the onEach extension function to the Array #KT-28290
This commit is contained in:
@@ -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 <T> Array<out T>.onEach(action: (T) -> Unit): Array<out T> {
|
||||
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.
|
||||
*
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -1416,6 +1416,31 @@ class ArraysTest {
|
||||
assertArrayNotSameButEquals(arrayOf("3", "2", "1"), (arrayOf("1", "2", "3") as Array<out String>).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<String>().apply { arrayOf("a", "b", "c").onEach { add(it) } })
|
||||
|
||||
assertEquals(listOf(1, 2, 3), mutableListOf<Int>().apply { intArrayOf(1, 2, 3).onEach { add(it) } })
|
||||
assertEquals(listOf<Byte>(1, 2, 3), mutableListOf<Byte>().apply { byteArrayOf(1, 2, 3).onEach { add(it) } })
|
||||
assertEquals(listOf<Short>(1, 2, 3), mutableListOf<Short>().apply { shortArrayOf(1, 2, 3).onEach { add(it) } })
|
||||
assertEquals(listOf<Long>(1, 2, 3), mutableListOf<Long>().apply { longArrayOf(1, 2, 3).onEach { add(it) } })
|
||||
assertEquals(listOf(1f, 2f, 3f), mutableListOf<Float>().apply { floatArrayOf(1f, 2f, 3f).onEach { add(it) } })
|
||||
assertEquals(listOf(1.0, 2.0, 3.0), mutableListOf<Double>().apply { doubleArrayOf(1.0, 2.0, 3.0).onEach { add(it) } })
|
||||
assertEquals(listOf(true, false, false), mutableListOf<Boolean>().apply { booleanArrayOf(true, false, false).onEach { add(it) } })
|
||||
assertEquals(listOf('1', '2', '3'), mutableListOf<Char>().apply { charArrayOf('1', '2', '3').onEach { add(it) } })
|
||||
}
|
||||
|
||||
@Test fun drop() {
|
||||
expect(listOf(1), { intArrayOf(1).drop(0) })
|
||||
expect(listOf(), { intArrayOf().drop(1) })
|
||||
|
||||
@@ -986,6 +986,14 @@ class UnsignedArraysTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onEach() {
|
||||
assertEquals(listOf<UInt>(1, 2, 3), mutableListOf<UInt>().apply { uintArrayOf(1, 2, 3).onEach { add(it) } })
|
||||
assertEquals(listOf<UByte>(1, 2, 3), mutableListOf<UByte>().apply { ubyteArrayOf(1, 2, 3).onEach { add(it) } })
|
||||
assertEquals(listOf<UShort>(1, 2, 3), mutableListOf<UShort>().apply { ushortArrayOf(1, 2, 3).onEach { add(it) } })
|
||||
assertEquals(listOf<ULong>(1, 2, 3), mutableListOf<ULong>().apply { ulongArrayOf(1, 2, 3).onEach { add(it) } })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun drop() {
|
||||
expect(listOf(1.toUByte())) { ubyteArrayOf(1).drop(0) }
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user