
kotlin.collections.Operations.kt Maven / Gradle / Ivy
package kotlin
import java.util.ArrayList
import kotlin.platform.platformName
/**
* Returns a single list of all elements from all collections in the given collection.
*/
public fun Iterable>.flatten(): List {
val result = ArrayList()
for (element in this) {
result.addAll(element)
}
return result
}
/**
* Returns a sequence of all elements from all sequences in this sequence.
*/
public fun Sequence>.flatten(): Sequence {
return MultiSequence(this)
}
/**
* Returns a single list of all elements from all arrays in the given array.
*/
public fun Array>.flatten(): List {
val result = ArrayList(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 Iterable>.unzip(): Pair, List> {
val expectedSize = collectionSizeOrDefault(10)
val listT = ArrayList(expectedSize)
val listR = ArrayList(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 Array>.unzip(): Pair, List> {
val listT = ArrayList(size())
val listR = ArrayList(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 Sequence>.unzip(): Pair, List> {
val listT = ArrayList()
val listR = ArrayList()
for (pair in this) {
listT.add(pair.first)
listR.add(pair.second)
}
return listT to listR
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy