diff --git a/libraries/stdlib/src/generated/_Mapping.kt b/libraries/stdlib/src/generated/_Mapping.kt index 916c948da2d..775428ea960 100644 --- a/libraries/stdlib/src/generated/_Mapping.kt +++ b/libraries/stdlib/src/generated/_Mapping.kt @@ -560,6 +560,233 @@ public inline fun String.map(transform: (Char) -> R): List { return mapTo(ArrayList(), transform) } +/** + * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection + */ +public inline fun Array.mapIndexed(transform: (Int, T) -> R): List { + return mapIndexedTo(ArrayList(), transform) +} + +/** + * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection + */ +public inline fun BooleanArray.mapIndexed(transform: (Int, Boolean) -> R): List { + return mapIndexedTo(ArrayList(), transform) +} + +/** + * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection + */ +public inline fun ByteArray.mapIndexed(transform: (Int, Byte) -> R): List { + return mapIndexedTo(ArrayList(), transform) +} + +/** + * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection + */ +public inline fun CharArray.mapIndexed(transform: (Int, Char) -> R): List { + return mapIndexedTo(ArrayList(), transform) +} + +/** + * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection + */ +public inline fun DoubleArray.mapIndexed(transform: (Int, Double) -> R): List { + return mapIndexedTo(ArrayList(), transform) +} + +/** + * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection + */ +public inline fun FloatArray.mapIndexed(transform: (Int, Float) -> R): List { + return mapIndexedTo(ArrayList(), transform) +} + +/** + * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection + */ +public inline fun IntArray.mapIndexed(transform: (Int, Int) -> R): List { + return mapIndexedTo(ArrayList(), transform) +} + +/** + * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection + */ +public inline fun LongArray.mapIndexed(transform: (Int, Long) -> R): List { + return mapIndexedTo(ArrayList(), transform) +} + +/** + * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection + */ +public inline fun ShortArray.mapIndexed(transform: (Int, Short) -> R): List { + return mapIndexedTo(ArrayList(), transform) +} + +/** + * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection + */ +public inline fun Iterable.mapIndexed(transform: (Int, T) -> R): List { + return mapIndexedTo(ArrayList(), transform) +} + +/** + * Returns a stream containing the results of applying the given *transform* function to each element and its index of the original stream + */ +public fun Stream.mapIndexed(transform: (Int, T) -> R): Stream { + return TransformingIndexedStream(this, transform) +} + +/** + * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection + */ +public inline fun String.mapIndexed(transform: (Int, Char) -> R): List { + return mapIndexedTo(ArrayList(), transform) +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > Array.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > BooleanArray.mapIndexedTo(destination: C, transform: (Int, Boolean) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > ByteArray.mapIndexedTo(destination: C, transform: (Int, Byte) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > CharArray.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > DoubleArray.mapIndexedTo(destination: C, transform: (Int, Double) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > FloatArray.mapIndexedTo(destination: C, transform: (Int, Float) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > IntArray.mapIndexedTo(destination: C, transform: (Int, Int) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > LongArray.mapIndexedTo(destination: C, transform: (Int, Long) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > ShortArray.mapIndexedTo(destination: C, transform: (Int, Short) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > Iterable.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > Map.mapIndexedTo(destination: C, transform: (Int, Map.Entry) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > Stream.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given *transform* function + * to the given *destination* + */ +public inline fun > String.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + /** * Returns a list containing the results of applying the given *transform* function to each non-null element of the original collection */ @@ -621,7 +848,7 @@ public inline fun > Stream.mapNotNul } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > Array.mapTo(destination: C, transform: (T) -> R): C { @@ -631,7 +858,7 @@ public inline fun > Array.mapTo(destina } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > BooleanArray.mapTo(destination: C, transform: (Boolean) -> R): C { @@ -641,7 +868,7 @@ public inline fun > BooleanArray.mapTo(destinatio } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > ByteArray.mapTo(destination: C, transform: (Byte) -> R): C { @@ -651,7 +878,7 @@ public inline fun > ByteArray.mapTo(destination: } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > CharArray.mapTo(destination: C, transform: (Char) -> R): C { @@ -661,7 +888,7 @@ public inline fun > CharArray.mapTo(destination: } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > DoubleArray.mapTo(destination: C, transform: (Double) -> R): C { @@ -671,7 +898,7 @@ public inline fun > DoubleArray.mapTo(destination } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > FloatArray.mapTo(destination: C, transform: (Float) -> R): C { @@ -681,7 +908,7 @@ public inline fun > FloatArray.mapTo(destination: } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > IntArray.mapTo(destination: C, transform: (Int) -> R): C { @@ -691,7 +918,7 @@ public inline fun > IntArray.mapTo(destination: C } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > LongArray.mapTo(destination: C, transform: (Long) -> R): C { @@ -701,7 +928,7 @@ public inline fun > LongArray.mapTo(destination: } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > ShortArray.mapTo(destination: C, transform: (Short) -> R): C { @@ -711,7 +938,7 @@ public inline fun > ShortArray.mapTo(destination: } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > Iterable.mapTo(destination: C, transform: (T) -> R): C { @@ -721,7 +948,7 @@ public inline fun > Iterable.mapTo(destinat } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > Map.mapTo(destination: C, transform: (Map.Entry) -> R): C { @@ -731,7 +958,7 @@ public inline fun > Map.mapTo(destina } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > Stream.mapTo(destination: C, transform: (T) -> R): C { @@ -741,7 +968,7 @@ public inline fun > Stream.mapTo(destinatio } /** - * Appends transformed elements of original collection using the given *transform* function + * Appends transformed elements of the original collection using the given *transform* function * to the given *destination* */ public inline fun > String.mapTo(destination: C, transform: (Char) -> R): C { diff --git a/libraries/stdlib/src/kotlin/collections/Stream.kt b/libraries/stdlib/src/kotlin/collections/Stream.kt index 94fc1f14898..66e0fc1002e 100644 --- a/libraries/stdlib/src/kotlin/collections/Stream.kt +++ b/libraries/stdlib/src/kotlin/collections/Stream.kt @@ -65,6 +65,19 @@ public class TransformingStream(private val stream: Stream, private val } } +public class TransformingIndexedStream(private val stream: Stream, private val transformer: (Int, T) -> R) : Stream { + override fun iterator(): Iterator = object : Iterator { + val iterator = stream.iterator() + var index = 0 + override fun next(): R { + return transformer(index++, iterator.next()) + } + override fun hasNext(): Boolean { + return iterator.hasNext() + } + } +} + public class IndexingStream(private val stream: Stream) : Stream> { override fun iterator(): Iterator> = object : Iterator> { val iterator = stream.iterator() diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index 58ca9816691..83b665c228c 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -15,7 +15,7 @@ class IterableWrapper(collection: Iterable) : Iterable { } class IterableTest : IterableTests>(iterableOf("foo", "bar"), iterableOf()) -class SetTest : IterableTests>(hashSetOf("foo", "bar"), hashSetOf()) +class SetTest : IterableTests>(setOf("foo", "bar"), setOf()) class ListTest : OrderedIterableTests>(listOf("foo", "bar"), listOf()) class ArrayListTest : OrderedIterableTests>(arrayListOf("foo", "bar"), arrayListOf()) diff --git a/libraries/stdlib/test/iterators/IteratorsTest.kt b/libraries/stdlib/test/iterators/IteratorsTest.kt index 44ce73ff9c8..b66d28889ef 100644 --- a/libraries/stdlib/test/iterators/IteratorsTest.kt +++ b/libraries/stdlib/test/iterators/IteratorsTest.kt @@ -30,6 +30,10 @@ class IteratorsTest { assertEquals(arrayListOf(0, 3, 3, 6, 9, 15), fibonacci().map { it * 3 }.takeWhile { (i: Int) -> i < 20 }.toList()) } + test fun mapIndexed() { + assertEquals(arrayListOf(0, 1, 2, 6, 12), fibonacci().mapIndexed { index, value -> index * value }.takeWhile {(i: Int) -> i < 20 }.toList()) + } + test fun joinConcatenatesTheFirstNElementsAboveAThreshold() { assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.joinToString(separator = ", ", limit = 5)) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt index 94746626598..1e14918f48c 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt @@ -44,6 +44,24 @@ fun mapping(): List { } } + templates add f("mapIndexed(transform: (Int, T) -> R)") { + inline(true) + + doc { "Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection" } + typeParam("R") + returns("List") + body { + "return mapIndexedTo(ArrayList(), transform)" + } + + inline(false, Streams) + returns(Streams) { "Stream" } + doc(Streams) { "Returns a stream containing the results of applying the given *transform* function to each element and its index of the original stream" } + body(Streams) { + "return TransformingIndexedStream(this, transform)" + } + } + templates add f("map(transform: (T) -> R)") { inline(true) @@ -92,7 +110,7 @@ fun mapping(): List { doc { """ - Appends transformed elements of original collection using the given *transform* function + Appends transformed elements of the original collection using the given *transform* function to the given *destination* """ } @@ -110,6 +128,30 @@ fun mapping(): List { include(Maps) } + templates add f("mapIndexedTo(destination: C, transform: (Int, T) -> R)") { + inline(true) + + doc { + """ + Appends transformed elements and their indices of the original collection using the given *transform* function + to the given *destination* + """ + } + typeParam("R") + typeParam("C : MutableCollection") + returns("C") + + body { + """ + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination + """ + } + include(Maps) + } + templates add f("mapNotNullTo(destination: C, transform: (T) -> R)") { inline(true) exclude(Strings, ArraysOfPrimitives)