Add onEachIndexed similar to forEachIndexed #KT-37161

This commit is contained in:
Abduqodiri Qurbonzoda
2020-04-01 02:52:25 +03:00
parent 57d390bab0
commit 4a7b1b210a
14 changed files with 368 additions and 17 deletions
@@ -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 <T> Array<out T>.forEachIndexed(action: (index: Int, T) -> Unit): Unit {
var index = 0
@@ -12336,7 +12336,7 @@ public inline fun <T> Array<out T>.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 <T> Array<out T>.onEachIndexed(action: (index: Int, T) -> Unit): Array<out T> {
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.
*
@@ -1666,7 +1666,7 @@ public inline fun <T> Iterable<T>.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 <T> Iterable<T>.forEachIndexed(action: (index: Int, T) -> Unit): Unit {
var index = 0
@@ -1878,6 +1878,17 @@ public inline fun <T, C : Iterable<T>> 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 <T, C : Iterable<T>> 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.
*
@@ -213,6 +213,17 @@ public inline fun <K, V, M : Map<out K, V>> M.onEach(action: (Map.Entry<K, V>) -
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 <K, V, M : Map<out K, V>> M.onEachIndexed(action: (index: Int, Map.Entry<K, V>) -> Unit): M {
return apply { entries.forEachIndexed(action) }
}
/**
* Creates an [Iterable] instance that wraps the original map returning its entries when being iterated.
*/
@@ -1115,7 +1115,7 @@ public inline fun <T> Sequence<T>.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 <T> Sequence<T>.onEach(action: (T) -> Unit): Sequence<T> {
}
}
/**
* 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 <T> Sequence<T>.onEachIndexed(action: (index: Int, T) -> Unit): Sequence<T> {
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.
*
@@ -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 : CharSequence> 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 : CharSequence> 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.
*
@@ -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.
*