Add mapIndexed() function for common case withIndex().map{} #KT-3153 Fixed

This commit is contained in:
Ilya Ryzhenkov
2014-12-15 21:53:34 +03:00
parent 846014b37f
commit f4b96a0a1a
5 changed files with 301 additions and 15 deletions
+240 -13
View File
@@ -560,6 +560,233 @@ public inline fun <R> String.map(transform: (Char) -> R): List<R> {
return mapTo(ArrayList<R>(), 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 <T, R> Array<out T>.mapIndexed(transform: (Int, T) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), 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 <R> BooleanArray.mapIndexed(transform: (Int, Boolean) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), 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 <R> ByteArray.mapIndexed(transform: (Int, Byte) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), 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 <R> CharArray.mapIndexed(transform: (Int, Char) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), 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 <R> DoubleArray.mapIndexed(transform: (Int, Double) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), 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 <R> FloatArray.mapIndexed(transform: (Int, Float) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), 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 <R> IntArray.mapIndexed(transform: (Int, Int) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), 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 <R> LongArray.mapIndexed(transform: (Int, Long) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), 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 <R> ShortArray.mapIndexed(transform: (Int, Short) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), 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 <T, R> Iterable<T>.mapIndexed(transform: (Int, T) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), 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 <T, R> Stream<T>.mapIndexed(transform: (Int, T) -> R): Stream<R> {
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 <R> String.mapIndexed(transform: (Int, Char) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
}
/**
* Appends transformed elements and their indices of the original collection using the given *transform* function
* 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
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 <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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 <T, R, C : MutableCollection<in R>> Iterable<T>.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 <K, V, R, C : MutableCollection<in R>> Map<K, V>.mapIndexedTo(destination: C, transform: (Int, Map.Entry<K, V>) -> 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 <T, R, C : MutableCollection<in R>> Stream<T>.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 <R, C : MutableCollection<in R>> 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 <T : Any, R, C : MutableCollection<in R>> Stream<T?>.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 <T, R, C : MutableCollection<in R>> Array<out T>.mapTo(destination: C, transform: (T) -> R): C {
@@ -631,7 +858,7 @@ public inline fun <T, R, C : MutableCollection<in R>> Array<out T>.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 <R, C : MutableCollection<in R>> BooleanArray.mapTo(destination: C, transform: (Boolean) -> R): C {
@@ -641,7 +868,7 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> ByteArray.mapTo(destination: C, transform: (Byte) -> R): C {
@@ -651,7 +878,7 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> CharArray.mapTo(destination: C, transform: (Char) -> R): C {
@@ -661,7 +888,7 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> DoubleArray.mapTo(destination: C, transform: (Double) -> R): C {
@@ -671,7 +898,7 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> FloatArray.mapTo(destination: C, transform: (Float) -> R): C {
@@ -681,7 +908,7 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> IntArray.mapTo(destination: C, transform: (Int) -> R): C {
@@ -691,7 +918,7 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> LongArray.mapTo(destination: C, transform: (Long) -> R): C {
@@ -701,7 +928,7 @@ public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> ShortArray.mapTo(destination: C, transform: (Short) -> R): C {
@@ -711,7 +938,7 @@ public inline fun <R, C : MutableCollection<in R>> 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 <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo(destination: C, transform: (T) -> R): C {
@@ -721,7 +948,7 @@ public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.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 <K, V, R, C : MutableCollection<in R>> Map<K, V>.mapTo(destination: C, transform: (Map.Entry<K, V>) -> R): C {
@@ -731,7 +958,7 @@ public inline fun <K, V, R, C : MutableCollection<in R>> Map<K, V>.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 <T, R, C : MutableCollection<in R>> Stream<T>.mapTo(destination: C, transform: (T) -> R): C {
@@ -741,7 +968,7 @@ public inline fun <T, R, C : MutableCollection<in R>> Stream<T>.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 <R, C : MutableCollection<in R>> String.mapTo(destination: C, transform: (Char) -> R): C {