diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 13cf0acc89f..a296402a048 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -6668,134 +6668,169 @@ public inline fun ShortArray.groupByTo(map: MutableMap } /** - * Returns a list containing the results of applying the given [transform] function to each element of the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. */ public inline fun Array.map(transform: (T) -> R): List { return mapTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element of the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. */ public inline fun BooleanArray.map(transform: (Boolean) -> R): List { return mapTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element of the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. */ public inline fun ByteArray.map(transform: (Byte) -> R): List { return mapTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element of the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. */ public inline fun CharArray.map(transform: (Char) -> R): List { return mapTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element of the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. */ public inline fun DoubleArray.map(transform: (Double) -> R): List { return mapTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element of the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. */ public inline fun FloatArray.map(transform: (Float) -> R): List { return mapTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element of the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. */ public inline fun IntArray.map(transform: (Int) -> R): List { return mapTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element of the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. */ public inline fun LongArray.map(transform: (Long) -> R): List { return mapTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element of the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. */ public inline fun ShortArray.map(transform: (Short) -> R): List { return mapTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element and its index in the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. */ public inline fun Array.mapIndexed(transform: (Int, T) -> R): List { return mapIndexedTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element and its index in the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. */ public inline fun BooleanArray.mapIndexed(transform: (Int, Boolean) -> R): List { return mapIndexedTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element and its index in the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. */ public inline fun ByteArray.mapIndexed(transform: (Int, Byte) -> R): List { return mapIndexedTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element and its index in the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. */ public inline fun CharArray.mapIndexed(transform: (Int, Char) -> R): List { return mapIndexedTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element and its index in the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. */ public inline fun DoubleArray.mapIndexed(transform: (Int, Double) -> R): List { return mapIndexedTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element and its index in the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. */ public inline fun FloatArray.mapIndexed(transform: (Int, Float) -> R): List { return mapIndexedTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element and its index in the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. */ public inline fun IntArray.mapIndexed(transform: (Int, Int) -> R): List { return mapIndexedTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element and its index in the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. */ public inline fun LongArray.mapIndexed(transform: (Int, Long) -> R): List { return mapIndexedTo(ArrayList(size()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element and its index in the original array. + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. */ public inline fun ShortArray.mapIndexed(transform: (Int, Short) -> R): List { return mapIndexedTo(ArrayList(size()), transform) } /** - * Appends transformed elements and their indices in the original array using the given [transform] function - * to the given [destination]. + * Returns a list containing only the non-null results of applying the given [transform] function + * to each element and its index in the original array. + */ +public inline fun Array.mapIndexedNotNull(transform: (Int, T) -> R?): List { + return mapIndexedNotNullTo(ArrayList(), transform) +} + +/** + * Applies the given [transform] function to each element and its index in the original array + * and appends only the non-null results to the given [destination]. + */ +public inline fun > Array.mapIndexedNotNullTo(destination: C, transform: (Int, T) -> R?): C { + forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } + return destination +} + +/** + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. */ public inline fun > Array.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { var index = 0 @@ -6805,8 +6840,8 @@ public inline fun > Array.mapIndexedTo( } /** - * Appends transformed elements and their indices in the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. */ public inline fun > BooleanArray.mapIndexedTo(destination: C, transform: (Int, Boolean) -> R): C { var index = 0 @@ -6816,8 +6851,8 @@ public inline fun > BooleanArray.mapIndexedTo(des } /** - * Appends transformed elements and their indices in the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. */ public inline fun > ByteArray.mapIndexedTo(destination: C, transform: (Int, Byte) -> R): C { var index = 0 @@ -6827,8 +6862,8 @@ public inline fun > ByteArray.mapIndexedTo(destin } /** - * Appends transformed elements and their indices in the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. */ public inline fun > CharArray.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C { var index = 0 @@ -6838,8 +6873,8 @@ public inline fun > CharArray.mapIndexedTo(destin } /** - * Appends transformed elements and their indices in the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. */ public inline fun > DoubleArray.mapIndexedTo(destination: C, transform: (Int, Double) -> R): C { var index = 0 @@ -6849,8 +6884,8 @@ public inline fun > DoubleArray.mapIndexedTo(dest } /** - * Appends transformed elements and their indices in the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. */ public inline fun > FloatArray.mapIndexedTo(destination: C, transform: (Int, Float) -> R): C { var index = 0 @@ -6860,8 +6895,8 @@ public inline fun > FloatArray.mapIndexedTo(desti } /** - * Appends transformed elements and their indices in the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. */ public inline fun > IntArray.mapIndexedTo(destination: C, transform: (Int, Int) -> R): C { var index = 0 @@ -6871,8 +6906,8 @@ public inline fun > IntArray.mapIndexedTo(destina } /** - * Appends transformed elements and their indices in the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. */ public inline fun > LongArray.mapIndexedTo(destination: C, transform: (Int, Long) -> R): C { var index = 0 @@ -6882,8 +6917,8 @@ public inline fun > LongArray.mapIndexedTo(destin } /** - * Appends transformed elements and their indices in the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. */ public inline fun > ShortArray.mapIndexedTo(destination: C, transform: (Int, Short) -> R): C { var index = 0 @@ -6893,8 +6928,25 @@ public inline fun > ShortArray.mapIndexedTo(desti } /** - * Appends transformed elements of the original array using the given [transform] function - * to the given [destination]. + * Returns a list containing only the non-null results of applying the given [transform] function + * to each element in the original array. + */ +public inline fun Array.mapNotNull(transform: (T) -> R?): List { + return mapNotNullTo(ArrayList(), transform) +} + +/** + * Applies the given [transform] function to each element in the original array + * and appends only the non-null results to the given [destination]. + */ +public inline fun > Array.mapNotNullTo(destination: C, transform: (T) -> R?): C { + forEach { element -> transform(element)?.let { destination.add(it) } } + return destination +} + +/** + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. */ public inline fun > Array.mapTo(destination: C, transform: (T) -> R): C { for (item in this) @@ -6903,8 +6955,8 @@ public inline fun > Array.mapTo(destina } /** - * Appends transformed elements of the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. */ public inline fun > BooleanArray.mapTo(destination: C, transform: (Boolean) -> R): C { for (item in this) @@ -6913,8 +6965,8 @@ public inline fun > BooleanArray.mapTo(destinatio } /** - * Appends transformed elements of the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. */ public inline fun > ByteArray.mapTo(destination: C, transform: (Byte) -> R): C { for (item in this) @@ -6923,8 +6975,8 @@ public inline fun > ByteArray.mapTo(destination: } /** - * Appends transformed elements of the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. */ public inline fun > CharArray.mapTo(destination: C, transform: (Char) -> R): C { for (item in this) @@ -6933,8 +6985,8 @@ public inline fun > CharArray.mapTo(destination: } /** - * Appends transformed elements of the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. */ public inline fun > DoubleArray.mapTo(destination: C, transform: (Double) -> R): C { for (item in this) @@ -6943,8 +6995,8 @@ public inline fun > DoubleArray.mapTo(destination } /** - * Appends transformed elements of the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. */ public inline fun > FloatArray.mapTo(destination: C, transform: (Float) -> R): C { for (item in this) @@ -6953,8 +7005,8 @@ public inline fun > FloatArray.mapTo(destination: } /** - * Appends transformed elements of the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. */ public inline fun > IntArray.mapTo(destination: C, transform: (Int) -> R): C { for (item in this) @@ -6963,8 +7015,8 @@ public inline fun > IntArray.mapTo(destination: C } /** - * Appends transformed elements of the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. */ public inline fun > LongArray.mapTo(destination: C, transform: (Long) -> R): C { for (item in this) @@ -6973,8 +7025,8 @@ public inline fun > LongArray.mapTo(destination: } /** - * Appends transformed elements of the original array using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. */ public inline fun > ShortArray.mapTo(destination: C, transform: (Short) -> R): C { for (item in this) diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 5421f4544e6..5e9d0d06139 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -1099,22 +1099,41 @@ public inline fun Iterable.groupByTo(map: MutableMap } /** - * Returns a list containing the results of applying the given [transform] function to each element of the original collection. + * Returns a list containing the results of applying the given [transform] function + * to each element in the original collection. */ public inline fun Iterable.map(transform: (T) -> R): List { return mapTo(ArrayList(collectionSizeOrDefault(10)), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each element and its index in the original collection. + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original collection. */ public inline fun Iterable.mapIndexed(transform: (Int, T) -> R): List { return mapIndexedTo(ArrayList(collectionSizeOrDefault(10)), transform) } /** - * Appends transformed elements and their indices in the original collection using the given [transform] function - * to the given [destination]. + * Returns a list containing only the non-null results of applying the given [transform] function + * to each element and its index in the original collection. + */ +public inline fun Iterable.mapIndexedNotNull(transform: (Int, T) -> R?): List { + return mapIndexedNotNullTo(ArrayList(), transform) +} + +/** + * Applies the given [transform] function to each element and its index in the original collection + * and appends only the non-null results to the given [destination]. + */ +public inline fun > Iterable.mapIndexedNotNullTo(destination: C, transform: (Int, T) -> R?): C { + forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } + return destination +} + +/** + * Applies the given [transform] function to each element and its index in the original collection + * and appends the results to the given [destination]. */ public inline fun > Iterable.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { var index = 0 @@ -1124,8 +1143,25 @@ public inline fun > Iterable.mapIndexedTo(d } /** - * Appends transformed elements of the original collection using the given [transform] function - * to the given [destination]. + * Returns a list containing only the non-null results of applying the given [transform] function + * to each element in the original collection. + */ +public inline fun Iterable.mapNotNull(transform: (T) -> R?): List { + return mapNotNullTo(ArrayList(), transform) +} + +/** + * Applies the given [transform] function to each element in the original collection + * and appends only the non-null results to the given [destination]. + */ +public inline fun > Iterable.mapNotNullTo(destination: C, transform: (T) -> R?): C { + forEach { element -> transform(element)?.let { destination.add(it) } } + return destination +} + +/** + * Applies the given [transform] function to each element of the original collection + * and appends the results to the given [destination]. */ public inline fun > Iterable.mapTo(destination: C, transform: (T) -> R): C { for (item in this) diff --git a/libraries/stdlib/src/generated/_Maps.kt b/libraries/stdlib/src/generated/_Maps.kt index 9b2fec44b91..967ba92acfd 100644 --- a/libraries/stdlib/src/generated/_Maps.kt +++ b/libraries/stdlib/src/generated/_Maps.kt @@ -41,15 +41,16 @@ public inline fun > Map.flatMapTo(des } /** - * Returns a list containing the results of applying the given [transform] function to each entry of the original map. + * Returns a list containing the results of applying the given [transform] function + * to each entry in the original map. */ public inline fun Map.map(transform: (Map.Entry) -> R): List { return mapTo(ArrayList(size()), transform) } /** - * Appends transformed entrys and their indices in the original map using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each entry and its index in the original map + * and appends the results to the given [destination]. */ public inline fun > Map.mapIndexedTo(destination: C, transform: (Int, Map.Entry) -> R): C { var index = 0 @@ -59,8 +60,25 @@ public inline fun > Map.mapIndexedTo( } /** - * Appends transformed entrys of the original map using the given [transform] function - * to the given [destination]. + * Returns a list containing only the non-null results of applying the given [transform] function + * to each entry in the original map. + */ +public inline fun Map.mapNotNull(transform: (Map.Entry) -> R?): List { + return mapNotNullTo(ArrayList(), transform) +} + +/** + * Applies the given [transform] function to each entry in the original map + * and appends only the non-null results to the given [destination]. + */ +public inline fun > Map.mapNotNullTo(destination: C, transform: (Map.Entry) -> R?): C { + forEach { element -> transform(element)?.let { destination.add(it) } } + return destination +} + +/** + * Applies the given [transform] function to each entry of the original map + * and appends the results to the given [destination]. */ public inline fun > Map.mapTo(destination: C, transform: (Map.Entry) -> R): C { for (item in this) diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 333a15b555f..074a408acbd 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -601,22 +601,41 @@ public inline fun Sequence.groupByTo(map: MutableMap } /** - * Returns a sequence containing the results of applying the given [transform] function to each element of the original sequence. + * Returns a sequence containing the results of applying the given [transform] function + * to each element in the original sequence. */ public fun Sequence.map(transform: (T) -> R): Sequence { return TransformingSequence(this, transform) } /** - * Returns a sequence containing the results of applying the given [transform] function to each element and its index in the original sequence. + * Returns a sequence containing the results of applying the given [transform] function + * to each element and its index in the original sequence. */ public fun Sequence.mapIndexed(transform: (Int, T) -> R): Sequence { return TransformingIndexedSequence(this, transform) } /** - * Appends transformed elements and their indices in the original sequence using the given [transform] function - * to the given [destination]. + * Returns a sequence containing only the non-null results of applying the given [transform] function + * to each element and its index in the original sequence. + */ +public fun Sequence.mapIndexedNotNull(transform: (Int, T) -> R?): Sequence { + return TransformingIndexedSequence(this, transform).filterNotNull() +} + +/** + * Applies the given [transform] function to each element and its index in the original sequence + * and appends only the non-null results to the given [destination]. + */ +public inline fun > Sequence.mapIndexedNotNullTo(destination: C, transform: (Int, T) -> R?): C { + forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } + return destination +} + +/** + * Applies the given [transform] function to each element and its index in the original sequence + * and appends the results to the given [destination]. */ public inline fun > Sequence.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { var index = 0 @@ -626,8 +645,25 @@ public inline fun > Sequence.mapIndexedTo(d } /** - * Appends transformed elements of the original sequence using the given [transform] function - * to the given [destination]. + * Returns a sequence containing only the non-null results of applying the given [transform] function + * to each element in the original sequence. + */ +public fun Sequence.mapNotNull(transform: (T) -> R?): Sequence { + return TransformingSequence(this, transform).filterNotNull() +} + +/** + * Applies the given [transform] function to each element in the original sequence + * and appends only the non-null results to the given [destination]. + */ +public inline fun > Sequence.mapNotNullTo(destination: C, transform: (T) -> R?): C { + forEach { element -> transform(element)?.let { destination.add(it) } } + return destination +} + +/** + * Applies the given [transform] function to each element of the original sequence + * and appends the results to the given [destination]. */ public inline fun > Sequence.mapTo(destination: C, transform: (T) -> R): C { for (item in this) diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index 2851d2f7b3f..f83a9bf1451 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -980,14 +980,16 @@ public inline fun String.groupByTo(map: MutableMap>, to } /** - * Returns a list containing the results of applying the given [transform] function to each character of the original char sequence. + * Returns a list containing the results of applying the given [transform] function + * to each character in the original char sequence. */ public inline fun CharSequence.map(transform: (Char) -> R): List { return mapTo(ArrayList(length()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each character of the original string. + * Returns a list containing the results of applying the given [transform] function + * to each character in the original string. */ @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public inline fun String.map(transform: (Char) -> R): List { @@ -995,14 +997,16 @@ public inline fun String.map(transform: (Char) -> R): List { } /** - * Returns a list containing the results of applying the given [transform] function to each character and its index in the original char sequence. + * Returns a list containing the results of applying the given [transform] function + * to each character and its index in the original char sequence. */ public inline fun CharSequence.mapIndexed(transform: (Int, Char) -> R): List { return mapIndexedTo(ArrayList(length()), transform) } /** - * Returns a list containing the results of applying the given [transform] function to each character and its index in the original string. + * Returns a list containing the results of applying the given [transform] function + * to each character and its index in the original string. */ @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public inline fun String.mapIndexed(transform: (Int, Char) -> R): List { @@ -1010,8 +1014,25 @@ public inline fun String.mapIndexed(transform: (Int, Char) -> R): List { } /** - * Appends transformed characters and their indices in the original char sequence using the given [transform] function - * to the given [destination]. + * Returns a list containing only the non-null results of applying the given [transform] function + * to each character and its index in the original char sequence. + */ +public inline fun CharSequence.mapIndexedNotNull(transform: (Int, Char) -> R?): List { + return mapIndexedNotNullTo(ArrayList(), transform) +} + +/** + * Applies the given [transform] function to each character and its index in the original char sequence + * and appends only the non-null results to the given [destination]. + */ +public inline fun > CharSequence.mapIndexedNotNullTo(destination: C, transform: (Int, Char) -> R?): C { + forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } + return destination +} + +/** + * Applies the given [transform] function to each character and its index in the original char sequence + * and appends the results to the given [destination]. */ public inline fun > CharSequence.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C { var index = 0 @@ -1021,8 +1042,8 @@ public inline fun > CharSequence.mapIndexedTo(des } /** - * Appends transformed characters and their indices in the original string using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each character and its index in the original string + * and appends the results to the given [destination]. */ @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public inline fun > String.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C { @@ -1033,8 +1054,25 @@ public inline fun > String.mapIndexedTo(destinati } /** - * Appends transformed characters of the original char sequence using the given [transform] function - * to the given [destination]. + * Returns a list containing only the non-null results of applying the given [transform] function + * to each character in the original char sequence. + */ +public inline fun CharSequence.mapNotNull(transform: (Char) -> R?): List { + return mapNotNullTo(ArrayList(), transform) +} + +/** + * Applies the given [transform] function to each character in the original char sequence + * and appends only the non-null results to the given [destination]. + */ +public inline fun > CharSequence.mapNotNullTo(destination: C, transform: (Char) -> R?): C { + forEach { element -> transform(element)?.let { destination.add(it) } } + return destination +} + +/** + * Applies the given [transform] function to each character of the original char sequence + * and appends the results to the given [destination]. */ public inline fun > CharSequence.mapTo(destination: C, transform: (Char) -> R): C { for (item in this) @@ -1043,8 +1081,8 @@ public inline fun > CharSequence.mapTo(destinatio } /** - * Appends transformed characters of the original string using the given [transform] function - * to the given [destination]. + * Applies the given [transform] function to each character of the original string + * and appends the results to the given [destination]. */ @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public inline fun > String.mapTo(destination: C, transform: (Char) -> R): C { diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index d862b0bac48..284783303a5 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -763,6 +763,27 @@ class ArraysTest { expect(listOf("a"), { arrayOf("a", null).filterNotNull() }) } + @test fun map() { + assertEquals(listOf(1, 2, 4), arrayOf("a", "bc", "test").map { it.length }) + assertEquals(listOf('a', 'b', 'c'), intArrayOf(1, 2, 3).map { 'a' + it - 1 }) + assertEquals(listOf(1, 2, 3), longArrayOf(1000, 2000, 3000).map { (it / 1000).toInt() }) + assertEquals(listOf(1.0, 0.5, 0.4, 0.2, 0.1), doubleArrayOf(1.0, 2.0, 2.5, 5.0, 10.0).map { 1 / it }) + } + + @test fun mapIndexed() { + assertEquals(listOf(1, 1, 2), arrayOf("a", "bc", "test").mapIndexed { index, s -> s.length - index }) + assertEquals(listOf(0, 2, 2), intArrayOf(3, 2, 1).mapIndexed { index, i -> i * index }) + assertEquals(listOf("0;20", "1;21", "2;22"), longArrayOf(20, 21, 22).mapIndexed { index, it -> "$index;$it" }) + } + + @test fun mapNotNull() { + assertEquals(listOf(2, 3), arrayOf("", "bc", "def").mapNotNull { if (it.isEmpty()) null else it.length }) + } + + @test fun mapIndexedNotNull() { + assertEquals(listOf(2), arrayOf("a", null, "test").mapIndexedNotNull { index, it -> it?.run { if (index != 0) length / index else null } }) + } + @test fun asListPrimitives() { // Array of primitives val arr = intArrayOf(1, 2, 3, 4, 2, 5) diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index 5ba52e14044..5ad6e2c67d8 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -244,6 +244,16 @@ abstract class IterableTests>(val data: T, val empty: T) { assertEquals(listOf("f", "ba"), indexed) } + @Test + fun mapNotNull() { + assertEquals(listOf('o'), data.mapNotNull { it.firstOrNull { c -> c in "oui" } }) + } + + @Test + fun mapIndexedNotNull() { + assertEquals(listOf('b'), data.mapIndexedNotNull { index, s -> s.getOrNull(index - 1) }) + } + @Test fun max() { expect("foo") { data.max() } diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index bc8c71250af..937357cc750 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -129,6 +129,13 @@ class MapTest { assertEquals(listOf("beer rocks", "Mells rocks"), list) } + + @test fun mapNotNull() { + val m1 = mapOf("a" to 1, "b" to null) + val list = m1.mapNotNull { it.value?.let { v -> "${it.key}$v" } } + assertEquals(listOf("a1"), list) + } + @test fun mapValues() { val m1 = mapOf("beverage" to "beer", "location" to "Mells") val m2 = m1.mapValues { it.value + "2" } diff --git a/libraries/stdlib/test/collections/SequenceTest.kt b/libraries/stdlib/test/collections/SequenceTest.kt index 8d98526e9ea..581f8438d53 100644 --- a/libraries/stdlib/test/collections/SequenceTest.kt +++ b/libraries/stdlib/test/collections/SequenceTest.kt @@ -6,15 +6,8 @@ import java.util.* fun fibonacci(): Sequence { // fibonacci terms - var index = 0; - var a = 0; - var b = 1 - return sequence { - when (index++) { 0 -> a; 1 -> b; else -> { - val result = a + b; a = b; b = result; result - } - } - } + // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, ... + return sequence(Pair(0, 1), { Pair(it.second, it.first + it.second) }).map { it.first } } public class SequenceTest { @@ -67,22 +60,22 @@ public class SequenceTest { assertEquals(listOf("foo", "bar"), filtered.toList()) } - /* - @test fun mapNotNull() { - val data = sequenceOf(null, "foo", null, "bar") - val foo = data.mapNotNull { it.length() } - assertEquals(listOf(3, 3), foo.toList()) - - assertTrue { - foo is Sequence - } - } - */ - @test fun mapIndexed() { assertEquals(listOf(0, 1, 2, 6, 12), fibonacci().mapIndexed { index, value -> index * value }.takeWhile { i: Int -> i < 20 }.toList()) } + @test fun mapNotNull() { + assertEquals(listOf(0, 10, 110, 1220), fibonacci().mapNotNull { if (it % 5 == 0) it * 2 else null }.take(4).toList()) + } + + @test fun mapIndexedNotNull() { + // find which terms are divisible by their index + assertEquals(listOf("1/1", "5/5", "144/12", "46368/24", "75025/25"), + fibonacci().mapIndexedNotNull { index, value -> + if (index > 0 && (value % index) == 0) "$value/$index" else null + }.take(5).toList()) + } + @test fun mapAndJoinToString() { assertEquals("3, 5, 8", fibonacci().withIndex().filter { it.index > 3 }.take(3).joinToString { it.value.toString() }) @@ -91,7 +84,6 @@ public class SequenceTest { @test fun withIndex() { val data = sequenceOf("foo", "bar") val indexed = data.withIndex().map { it.value.substring(0..it.index) }.toList() - assertEquals(2, indexed.size()) assertEquals(listOf("f", "ba"), indexed) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt index 5b2c5b8ba09..f1b75178403 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt @@ -30,7 +30,12 @@ fun mapping(): List { templates add f("mapIndexed(transform: (Int, T) -> R)") { inline(true) - doc { f -> "Returns a ${f.mapResult} containing the results of applying the given [transform] function to each ${f.element} and its index in the original ${f.collection}." } + doc { f -> + """ + Returns a ${f.mapResult} containing the results of applying the given [transform] function + to each ${f.element} and its index in the original ${f.collection}. + """ + } typeParam("R") returns("List") body { @@ -53,7 +58,12 @@ fun mapping(): List { templates add f("map(transform: (T) -> R)") { inline(true) - doc { f -> "Returns a ${f.mapResult} containing the results of applying the given [transform] function to each ${f.element} of the original ${f.collection}." } + doc { f -> + """ + Returns a ${f.mapResult} containing the results of applying the given [transform] function + to each ${f.element} in the original ${f.collection}. + """ + } typeParam("R") returns("List") body { @@ -75,39 +85,60 @@ fun mapping(): List { include(Maps) } - /* - templates add f("mapNotNull(transform: (T) -> R)") { + templates add f("mapNotNull(transform: (T) -> R?)") { inline(true) - exclude(Strings, ArraysOfPrimitives) - doc { f -> "Returns a ${f.mapResult} containing the results of applying the given [transform] function to each non-null ${f.element} of the original ${f.collection}." } - deprecate(Deprecation("This function will change its semantics soon to map&filter rather than filter&map. Use filterNotNull().map {} instead.", replaceWith = "filterNotNull().map(transform)")) - typeParam("T : Any") - typeParam("R") + include(Maps, CharSequences) + exclude(ArraysOfPrimitives) + typeParam("R : Any") returns("List") - toNullableT = true + doc { f -> + """ + Returns a ${f.mapResult} containing only the non-null results of applying the given [transform] function + to each ${f.element} in the original ${f.collection}. + """ + } body { - """ - return mapNotNullTo(ArrayList(), transform) - """ + "return mapNotNullTo(ArrayList(), transform)" } - returns(Sequences) { "Sequence" } inline(false, Sequences) + returns(Sequences) { "Sequence" } body(Sequences) { + "return TransformingSequence(this, transform).filterNotNull()" + } + + } + + templates add f("mapIndexedNotNull(transform: (Int, T) -> R?)") { + inline(true) + include(CharSequences) + exclude(ArraysOfPrimitives) + typeParam("R : Any") + returns("List") + doc { f -> """ - return TransformingSequence(FilteringSequence(this, false, { it == null }) as Sequence, transform) + Returns a ${f.mapResult} containing only the non-null results of applying the given [transform] function + to each ${f.element} and its index in the original ${f.collection}. """ } + body { + "return mapIndexedNotNullTo(ArrayList(), transform)" + } + + inline(false, Sequences) + returns(Sequences) { "Sequence" } + body(Sequences) { + "return TransformingIndexedSequence(this, transform).filterNotNull()" + } } - */ templates add f("mapTo(destination: C, transform: (T) -> R)") { inline(true) doc { f -> """ - Appends transformed ${f.element}s of the original ${f.collection} using the given [transform] function - to the given [destination]. + Applies the given [transform] function to each ${f.element} of the original ${f.collection} + and appends the results to the given [destination]. """ } typeParam("R") @@ -130,8 +161,8 @@ fun mapping(): List { doc { f -> """ - Appends transformed ${f.element}s and their indices in the original ${f.collection} using the given [transform] function - to the given [destination]. + Applies the given [transform] function to each ${f.element} and its index in the original ${f.collection} + and appends the results to the given [destination]. """ } typeParam("R") @@ -150,35 +181,47 @@ fun mapping(): List { include(Maps, CharSequences, Strings) } - /* - templates add f("mapNotNullTo(destination: C, transform: (T) -> R)") { + templates add f("mapNotNullTo(destination: C, transform: (T) -> R?)") { inline(true) - exclude(Strings, ArraysOfPrimitives) - doc { - """ - Appends transformed non-null elements of original collection using the given [transform] function - to the given [destination]. - """ - } - deprecate(Deprecation("This function will change its semantics soon to map&filter rather than filter&map. Use filterNotNull().mapTo(destination) {} instead.", - replaceWith = "filterNotNull().mapTo(destination, transform)")) - typeParam("T : Any") - typeParam("R") + include(Maps, CharSequences) + exclude(ArraysOfPrimitives) + typeParam("R : Any") typeParam("C : MutableCollection") returns("C") - toNullableT = true + doc { f -> + """ + Applies the given [transform] function to each ${f.element} in the original ${f.collection} + and appends only the non-null results to the given [destination]. + """ + } body { """ - for (element in this) { - if (element != null) { - destination.add(transform(element)) - } - } + forEach { element -> transform(element)?.let { destination.add(it) } } + return destination + """ + } + } + + templates add f("mapIndexedNotNullTo(destination: C, transform: (Int, T) -> R?)") { + inline(true) + include(CharSequences) + exclude(ArraysOfPrimitives) + typeParam("R : Any") + typeParam("C : MutableCollection") + returns("C") + doc { f -> + """ + Applies the given [transform] function to each ${f.element} and its index in the original ${f.collection} + and appends only the non-null results to the given [destination]. + """ + } + body { + """ + forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } return destination """ } } - */ templates add f("flatMap(transform: (T) -> Iterable)") { inline(true)