Move flatten and unzip to parts corresponding to the receiver type of operation.
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package kotlin
|
package kotlin
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array with the specified [size], where each element is calculated by calling the specified
|
* Returns an array with the specified [size], where each element is calculated by calling the specified
|
||||||
* [init] function. The `init` function returns an array element given its index.
|
* [init] function. The `init` function returns an array element given its index.
|
||||||
@@ -19,3 +21,28 @@ public inline fun <reified T> Array(size: Int, init: (Int) -> T): Array<T> {
|
|||||||
*/
|
*/
|
||||||
public inline fun <reified T> emptyArray(): Array<T> = arrayOfNulls<T>(0) as Array<T>
|
public inline fun <reified T> emptyArray(): Array<T> = arrayOfNulls<T>(0) as Array<T>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a single list of all elements from all arrays in the given array.
|
||||||
|
*/
|
||||||
|
public fun <T> Array<Array<out T>>.flatten(): List<T> {
|
||||||
|
val result = ArrayList<T>(sumBy { it.size() })
|
||||||
|
for (element in this) {
|
||||||
|
result.addAll(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a pair of lists, where
|
||||||
|
* *first* list is built from the first values of each pair from this array,
|
||||||
|
* *second* list is built from the second values of each pair from this array.
|
||||||
|
*/
|
||||||
|
public fun <T, R> Array<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
|
||||||
|
val listT = ArrayList<T>(size())
|
||||||
|
val listR = ArrayList<R>(size())
|
||||||
|
for (pair in this) {
|
||||||
|
listT.add(pair.first)
|
||||||
|
listR.add(pair.second)
|
||||||
|
}
|
||||||
|
return listT to listR
|
||||||
|
}
|
||||||
|
|||||||
@@ -246,3 +246,31 @@ private fun rangeCheck(size: Int, fromIndex: Int, toIndex: Int) {
|
|||||||
toIndex > size -> throw IndexOutOfBoundsException("toIndex ($toIndex) is greater than size ($size).")
|
toIndex > size -> throw IndexOutOfBoundsException("toIndex ($toIndex) is greater than size ($size).")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a single list of all elements from all collections in the given collection.
|
||||||
|
*/
|
||||||
|
public fun <T> Iterable<Iterable<T>>.flatten(): List<T> {
|
||||||
|
val result = ArrayList<T>()
|
||||||
|
for (element in this) {
|
||||||
|
result.addAll(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a pair of lists, where
|
||||||
|
* *first* list is built from the first values of each pair from this collection,
|
||||||
|
* *second* list is built from the second values of each pair from this collection.
|
||||||
|
*/
|
||||||
|
public fun <T, R> Iterable<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
|
||||||
|
val expectedSize = collectionSizeOrDefault(10)
|
||||||
|
val listT = ArrayList<T>(expectedSize)
|
||||||
|
val listR = ArrayList<R>(expectedSize)
|
||||||
|
for (pair in this) {
|
||||||
|
listT.add(pair.first)
|
||||||
|
listR.add(pair.second)
|
||||||
|
}
|
||||||
|
return listT to listR
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,78 +0,0 @@
|
|||||||
package kotlin
|
|
||||||
|
|
||||||
import java.util.ArrayList
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a single list of all elements from all collections in the given collection.
|
|
||||||
*/
|
|
||||||
public fun <T> Iterable<Iterable<T>>.flatten(): List<T> {
|
|
||||||
val result = ArrayList<T>()
|
|
||||||
for (element in this) {
|
|
||||||
result.addAll(element)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a sequence of all elements from all sequences in this sequence.
|
|
||||||
*/
|
|
||||||
public fun <T> Sequence<Sequence<T>>.flatten(): Sequence<T> {
|
|
||||||
return MultiSequence(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a single list of all elements from all arrays in the given array.
|
|
||||||
*/
|
|
||||||
public fun <T> Array<Array<out T>>.flatten(): List<T> {
|
|
||||||
val result = ArrayList<T>(sumBy { it.size() })
|
|
||||||
for (element in this) {
|
|
||||||
result.addAll(element)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a pair of lists, where
|
|
||||||
* *first* list is built from the first values of each pair from this collection,
|
|
||||||
* *second* list is built from the second values of each pair from this collection.
|
|
||||||
*/
|
|
||||||
public fun <T, R> Iterable<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
|
|
||||||
val expectedSize = collectionSizeOrDefault(10)
|
|
||||||
val listT = ArrayList<T>(expectedSize)
|
|
||||||
val listR = ArrayList<R>(expectedSize)
|
|
||||||
for (pair in this) {
|
|
||||||
listT.add(pair.first)
|
|
||||||
listR.add(pair.second)
|
|
||||||
}
|
|
||||||
return listT to listR
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a pair of lists, where
|
|
||||||
* *first* list is built from the first values of each pair from this array,
|
|
||||||
* *second* list is built from the second values of each pair from this array.
|
|
||||||
*/
|
|
||||||
public fun <T, R> Array<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
|
|
||||||
val listT = ArrayList<T>(size())
|
|
||||||
val listR = ArrayList<R>(size())
|
|
||||||
for (pair in this) {
|
|
||||||
listT.add(pair.first)
|
|
||||||
listR.add(pair.second)
|
|
||||||
}
|
|
||||||
return listT to listR
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a pair of lists, where
|
|
||||||
* *first* list is built from the first values of each pair from this sequence,
|
|
||||||
* *second* list is built from the second values of each pair from this sequence.
|
|
||||||
*/
|
|
||||||
public fun <T, R> Sequence<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
|
|
||||||
val listT = ArrayList<T>()
|
|
||||||
val listR = ArrayList<R>()
|
|
||||||
for (pair in this) {
|
|
||||||
listT.add(pair.first)
|
|
||||||
listR.add(pair.second)
|
|
||||||
}
|
|
||||||
return listT to listR
|
|
||||||
}
|
|
||||||
@@ -65,6 +65,27 @@ private object EmptySequence : Sequence<Nothing> {
|
|||||||
override fun iterator(): Iterator<Nothing> = EmptyIterator
|
override fun iterator(): Iterator<Nothing> = EmptyIterator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a sequence of all elements from all sequences in this sequence.
|
||||||
|
*/
|
||||||
|
public fun <T> Sequence<Sequence<T>>.flatten(): Sequence<T> {
|
||||||
|
return MultiSequence(this)
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a pair of lists, where
|
||||||
|
* *first* list is built from the first values of each pair from this sequence,
|
||||||
|
* *second* list is built from the second values of each pair from this sequence.
|
||||||
|
*/
|
||||||
|
public fun <T, R> Sequence<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
|
||||||
|
val listT = ArrayList<T>()
|
||||||
|
val listR = ArrayList<R>()
|
||||||
|
for (pair in this) {
|
||||||
|
listT.add(pair.first)
|
||||||
|
listR.add(pair.second)
|
||||||
|
}
|
||||||
|
return listT to listR
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A sequence that returns the values from the underlying [sequence] that either match or do not match
|
* A sequence that returns the values from the underlying [sequence] that either match or do not match
|
||||||
* the specified [predicate].
|
* the specified [predicate].
|
||||||
|
|||||||
Reference in New Issue
Block a user