Add onEachIndexed similar to forEachIndexed #KT-37161
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -1441,6 +1441,23 @@ class ArraysTest {
|
||||
assertEquals(listOf('1', '2', '3'), mutableListOf<Char>().apply { charArrayOf('1', '2', '3').onEach { add(it) } })
|
||||
}
|
||||
|
||||
@Test fun onEachIndexed() {
|
||||
assertEquals(listOf(1, 3, 5), mutableListOf<Int>().apply { intArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i + e) } })
|
||||
assertEquals(listOf(1, 3, 5), mutableListOf<Int>().apply { byteArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i + e) } })
|
||||
assertEquals(listOf(1, 3, 5), mutableListOf<Int>().apply { shortArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i + e) } })
|
||||
assertEquals(listOf<Long>(1, 3, 5), mutableListOf<Long>().apply { longArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i + e) } })
|
||||
assertEquals(listOf(1f, 3f, 5f), mutableListOf<Float>().apply { floatArrayOf(1f, 2f, 3f).onEachIndexed { i, e -> add(i + e) } })
|
||||
assertEquals(listOf(1.0, 3.0, 5.0), mutableListOf<Double>().apply { doubleArrayOf(1.0, 2.0, 3.0).onEachIndexed { i, e -> add(i + e) } })
|
||||
assertEquals(listOf(true, false, true), mutableListOf<Boolean>().apply { booleanArrayOf(true, false, false).onEachIndexed { i, e -> add(i % 2 == 0 || e) } })
|
||||
assertEquals(listOf('1', '3', '5'), mutableListOf<Char>().apply { charArrayOf('1', '2', '3').onEachIndexed { i, e -> add(e + i) } })
|
||||
assertEquals(listOf("a0", "b1", "c2"), mutableListOf<String>().apply { arrayOf("a", "b", "c").onEachIndexed { i, e -> add(e + i) } })
|
||||
|
||||
val empty = arrayOf<Int>()
|
||||
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) })
|
||||
|
||||
@@ -313,6 +313,17 @@ abstract class IterableTests<T : Iterable<String>>(val createFrom: (Array<out St
|
||||
assertStaticTypeIs<ArrayList<Int>>(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<ArrayList<Int>>(arrayListOf(1, 2, 3).onEachIndexed { _, _ -> })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun contains() {
|
||||
assertTrue(data.contains("foo"))
|
||||
|
||||
@@ -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<HashMap<String, String>>(
|
||||
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()
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -994,6 +994,21 @@ class UnsignedArraysTest {
|
||||
assertEquals(listOf<ULong>(1, 2, 3), mutableListOf<ULong>().apply { ulongArrayOf(1, 2, 3).onEach { add(it) } })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onEachIndexed() {
|
||||
assertEquals(listOf<UInt>(1, 3, 5), mutableListOf<UInt>().apply { uintArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toUInt() + e) } })
|
||||
assertEquals(listOf<UInt>(1, 3, 5), mutableListOf<UInt>().apply { ubyteArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toUByte() + e) } })
|
||||
assertEquals(listOf<UInt>(1, 3, 5), mutableListOf<UInt>().apply { ushortArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toUShort() + e) } })
|
||||
assertEquals(listOf<ULong>(1, 3, 5), mutableListOf<ULong>().apply { ulongArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toULong() + e) } })
|
||||
|
||||
val empty = arrayOf<UInt>()
|
||||
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) }
|
||||
|
||||
@@ -948,6 +948,18 @@ class StringTest {
|
||||
assertStaticTypeIs<StringBuilder>(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<String>("x".onEachIndexed { _, _ -> })
|
||||
assertStaticTypeIs<StringBuilder>(result.onEachIndexed { _, _ -> })
|
||||
}
|
||||
|
||||
|
||||
@Test fun filter() {
|
||||
assertEquals("acdca", ("abcdcba").filter { !it.equals('b') })
|
||||
|
||||
+4
@@ -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;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user