Introduce T.addTo(MutableCollection<T>), mapNotNull and mapIndexedNotNull extensions.

#KT-4410 Fixed
This commit is contained in:
Ilya Gorbunov
2015-11-07 06:50:19 +03:00
parent 008a8059cf
commit 9323f61eaf
10 changed files with 398 additions and 145 deletions
+106 -54
View File
@@ -6668,134 +6668,169 @@ public inline fun <K> ShortArray.groupByTo(map: MutableMap<K, MutableList<Short>
}
/**
* 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 <T, R> Array<out T>.map(transform: (T) -> R): List<R> {
return mapTo(ArrayList<R>(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 <R> BooleanArray.map(transform: (Boolean) -> R): List<R> {
return mapTo(ArrayList<R>(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 <R> ByteArray.map(transform: (Byte) -> R): List<R> {
return mapTo(ArrayList<R>(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 <R> CharArray.map(transform: (Char) -> R): List<R> {
return mapTo(ArrayList<R>(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 <R> DoubleArray.map(transform: (Double) -> R): List<R> {
return mapTo(ArrayList<R>(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 <R> FloatArray.map(transform: (Float) -> R): List<R> {
return mapTo(ArrayList<R>(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 <R> IntArray.map(transform: (Int) -> R): List<R> {
return mapTo(ArrayList<R>(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 <R> LongArray.map(transform: (Long) -> R): List<R> {
return mapTo(ArrayList<R>(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 <R> ShortArray.map(transform: (Short) -> R): List<R> {
return mapTo(ArrayList<R>(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 <T, R> Array<out T>.mapIndexed(transform: (Int, T) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(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 <R> BooleanArray.mapIndexed(transform: (Int, Boolean) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(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 <R> ByteArray.mapIndexed(transform: (Int, Byte) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(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 <R> CharArray.mapIndexed(transform: (Int, Char) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(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 <R> DoubleArray.mapIndexed(transform: (Int, Double) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(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 <R> FloatArray.mapIndexed(transform: (Int, Float) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(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 <R> IntArray.mapIndexed(transform: (Int, Int) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(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 <R> LongArray.mapIndexed(transform: (Int, Long) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(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 <R> ShortArray.mapIndexed(transform: (Int, Short) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(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 <T, R : Any> Array<out T>.mapIndexedNotNull(transform: (Int, T) -> R?): List<R> {
return mapIndexedNotNullTo(ArrayList<R>(), 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 <T, R : Any, C : MutableCollection<in R>> Array<out T>.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 <T, R, C : MutableCollection<in R>> Array<out T>.mapIndexedTo(destination: C, transform: (Int, T) -> R): C {
var index = 0
@@ -6805,8 +6840,8 @@ public inline fun <T, R, C : MutableCollection<in R>> Array<out T>.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 <R, C : MutableCollection<in R>> BooleanArray.mapIndexedTo(destination: C, transform: (Int, Boolean) -> R): C {
var index = 0
@@ -6816,8 +6851,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> ByteArray.mapIndexedTo(destination: C, transform: (Int, Byte) -> R): C {
var index = 0
@@ -6827,8 +6862,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> CharArray.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C {
var index = 0
@@ -6838,8 +6873,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> DoubleArray.mapIndexedTo(destination: C, transform: (Int, Double) -> R): C {
var index = 0
@@ -6849,8 +6884,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> FloatArray.mapIndexedTo(destination: C, transform: (Int, Float) -> R): C {
var index = 0
@@ -6860,8 +6895,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> IntArray.mapIndexedTo(destination: C, transform: (Int, Int) -> R): C {
var index = 0
@@ -6871,8 +6906,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> LongArray.mapIndexedTo(destination: C, transform: (Int, Long) -> R): C {
var index = 0
@@ -6882,8 +6917,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> ShortArray.mapIndexedTo(destination: C, transform: (Int, Short) -> R): C {
var index = 0
@@ -6893,8 +6928,25 @@ public inline fun <R, C : MutableCollection<in R>> 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 <T, R : Any> Array<out T>.mapNotNull(transform: (T) -> R?): List<R> {
return mapNotNullTo(ArrayList<R>(), 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 <T, R : Any, C : MutableCollection<in R>> Array<out T>.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 <T, R, C : MutableCollection<in R>> Array<out T>.mapTo(destination: C, transform: (T) -> R): C {
for (item in this)
@@ -6903,8 +6955,8 @@ public inline fun <T, R, C : MutableCollection<in R>> Array<out T>.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 <R, C : MutableCollection<in R>> BooleanArray.mapTo(destination: C, transform: (Boolean) -> R): C {
for (item in this)
@@ -6913,8 +6965,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> ByteArray.mapTo(destination: C, transform: (Byte) -> R): C {
for (item in this)
@@ -6923,8 +6975,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> CharArray.mapTo(destination: C, transform: (Char) -> R): C {
for (item in this)
@@ -6933,8 +6985,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> DoubleArray.mapTo(destination: C, transform: (Double) -> R): C {
for (item in this)
@@ -6943,8 +6995,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> FloatArray.mapTo(destination: C, transform: (Float) -> R): C {
for (item in this)
@@ -6953,8 +7005,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> IntArray.mapTo(destination: C, transform: (Int) -> R): C {
for (item in this)
@@ -6963,8 +7015,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> LongArray.mapTo(destination: C, transform: (Long) -> R): C {
for (item in this)
@@ -6973,8 +7025,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> ShortArray.mapTo(destination: C, transform: (Short) -> R): C {
for (item in this)
+42 -6
View File
@@ -1099,22 +1099,41 @@ public inline fun <T, K> Iterable<T>.groupByTo(map: MutableMap<K, MutableList<T>
}
/**
* 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 <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
return mapTo(ArrayList<R>(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 <T, R> Iterable<T>.mapIndexed(transform: (Int, T) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(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 <T, R : Any> Iterable<T>.mapIndexedNotNull(transform: (Int, T) -> R?): List<R> {
return mapIndexedNotNullTo(ArrayList<R>(), 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 <T, R : Any, C : MutableCollection<in R>> Iterable<T>.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 <T, R, C : MutableCollection<in R>> Iterable<T>.mapIndexedTo(destination: C, transform: (Int, T) -> R): C {
var index = 0
@@ -1124,8 +1143,25 @@ public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.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 <T, R : Any> Iterable<T>.mapNotNull(transform: (T) -> R?): List<R> {
return mapNotNullTo(ArrayList<R>(), 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 <T, R : Any, C : MutableCollection<in R>> Iterable<T>.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 <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo(destination: C, transform: (T) -> R): C {
for (item in this)
+23 -5
View File
@@ -41,15 +41,16 @@ public inline fun <K, V, R, C : MutableCollection<in R>> Map<K, V>.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 <K, V, R> Map<K, V>.map(transform: (Map.Entry<K, V>) -> R): List<R> {
return mapTo(ArrayList<R>(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 <K, V, R, C : MutableCollection<in R>> Map<K, V>.mapIndexedTo(destination: C, transform: (Int, Map.Entry<K, V>) -> R): C {
var index = 0
@@ -59,8 +60,25 @@ public inline fun <K, V, R, C : MutableCollection<in R>> Map<K, V>.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 <K, V, R : Any> Map<K, V>.mapNotNull(transform: (Map.Entry<K, V>) -> R?): List<R> {
return mapNotNullTo(ArrayList<R>(), 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 <K, V, R : Any, C : MutableCollection<in R>> Map<K, V>.mapNotNullTo(destination: C, transform: (Map.Entry<K, V>) -> 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 <K, V, R, C : MutableCollection<in R>> Map<K, V>.mapTo(destination: C, transform: (Map.Entry<K, V>) -> R): C {
for (item in this)
+42 -6
View File
@@ -601,22 +601,41 @@ public inline fun <T, K> Sequence<T>.groupByTo(map: MutableMap<K, MutableList<T>
}
/**
* 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 <T, R> Sequence<T>.map(transform: (T) -> R): Sequence<R> {
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 <T, R> Sequence<T>.mapIndexed(transform: (Int, T) -> R): Sequence<R> {
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 <T, R : Any> Sequence<T>.mapIndexedNotNull(transform: (Int, T) -> R?): Sequence<R> {
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 <T, R : Any, C : MutableCollection<in R>> Sequence<T>.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 <T, R, C : MutableCollection<in R>> Sequence<T>.mapIndexedTo(destination: C, transform: (Int, T) -> R): C {
var index = 0
@@ -626,8 +645,25 @@ public inline fun <T, R, C : MutableCollection<in R>> Sequence<T>.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 <T, R : Any> Sequence<T>.mapNotNull(transform: (T) -> R?): Sequence<R> {
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 <T, R : Any, C : MutableCollection<in R>> Sequence<T>.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 <T, R, C : MutableCollection<in R>> Sequence<T>.mapTo(destination: C, transform: (T) -> R): C {
for (item in this)
+50 -12
View File
@@ -980,14 +980,16 @@ public inline fun <K> String.groupByTo(map: MutableMap<K, MutableList<Char>>, 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 <R> CharSequence.map(transform: (Char) -> R): List<R> {
return mapTo(ArrayList<R>(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 <R> String.map(transform: (Char) -> R): List<R> {
@@ -995,14 +997,16 @@ public inline fun <R> String.map(transform: (Char) -> R): List<R> {
}
/**
* 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 <R> CharSequence.mapIndexed(transform: (Int, Char) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(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 <R> String.mapIndexed(transform: (Int, Char) -> R): List<R> {
@@ -1010,8 +1014,25 @@ public inline fun <R> String.mapIndexed(transform: (Int, Char) -> R): List<R> {
}
/**
* 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 <R : Any> CharSequence.mapIndexedNotNull(transform: (Int, Char) -> R?): List<R> {
return mapIndexedNotNullTo(ArrayList<R>(), 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 <R : Any, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> CharSequence.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C {
var index = 0
@@ -1021,8 +1042,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> String.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C {
@@ -1033,8 +1054,25 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R : Any> CharSequence.mapNotNull(transform: (Char) -> R?): List<R> {
return mapNotNullTo(ArrayList<R>(), 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 <R : Any, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> CharSequence.mapTo(destination: C, transform: (Char) -> R): C {
for (item in this)
@@ -1043,8 +1081,8 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> String.mapTo(destination: C, transform: (Char) -> R): C {
@@ -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)
@@ -244,6 +244,16 @@ abstract class IterableTests<T : Iterable<String>>(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() }
@@ -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" }
@@ -6,15 +6,8 @@ import java.util.*
fun fibonacci(): Sequence<Int> {
// 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<Int>
}
}
*/
@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)
}
@@ -30,7 +30,12 @@ fun mapping(): List<GenericFunction> {
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<R>")
body {
@@ -53,7 +58,12 @@ fun mapping(): List<GenericFunction> {
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<R>")
body {
@@ -75,39 +85,60 @@ fun mapping(): List<GenericFunction> {
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<R>")
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<R>(), transform)
"""
"return mapNotNullTo(ArrayList<R>(), transform)"
}
returns(Sequences) { "Sequence<R>" }
inline(false, Sequences)
returns(Sequences) { "Sequence<R>" }
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<R>")
doc { f ->
"""
return TransformingSequence(FilteringSequence(this, false, { it == null }) as Sequence<T>, 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<R>(), transform)"
}
inline(false, Sequences)
returns(Sequences) { "Sequence<R>" }
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<GenericFunction> {
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<GenericFunction> {
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<in R>")
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<in R>")
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<R>)") {
inline(true)