diff --git a/libraries/stdlib/src/kotlin/collections/Arrays.kt b/libraries/stdlib/src/kotlin/collections/Arrays.kt index 85bf5ccf57a..61d8fc27b6b 100644 --- a/libraries/stdlib/src/kotlin/collections/Arrays.kt +++ b/libraries/stdlib/src/kotlin/collections/Arrays.kt @@ -1,5 +1,7 @@ package kotlin +import java.util.* + /** * 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. @@ -19,3 +21,28 @@ public inline fun Array(size: Int, init: (Int) -> T): Array { */ public inline fun emptyArray(): Array = arrayOfNulls(0) as Array +/** + * 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 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 +} diff --git a/libraries/stdlib/src/kotlin/collections/JUtil.kt b/libraries/stdlib/src/kotlin/collections/JUtil.kt index c3d5441b85d..0946b53eae1 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtil.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtil.kt @@ -246,3 +246,31 @@ private fun rangeCheck(size: Int, fromIndex: Int, toIndex: Int) { 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 Iterable>.flatten(): List { + val result = ArrayList() + 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 +} + diff --git a/libraries/stdlib/src/kotlin/collections/Operations.kt b/libraries/stdlib/src/kotlin/collections/Operations.kt deleted file mode 100644 index 49000f3eadb..00000000000 --- a/libraries/stdlib/src/kotlin/collections/Operations.kt +++ /dev/null @@ -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 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 -} \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/collections/Sequence.kt b/libraries/stdlib/src/kotlin/collections/Sequence.kt index 16d9d9c49a7..c9e96804fb6 100644 --- a/libraries/stdlib/src/kotlin/collections/Sequence.kt +++ b/libraries/stdlib/src/kotlin/collections/Sequence.kt @@ -65,6 +65,27 @@ private object EmptySequence : Sequence { override fun iterator(): Iterator = EmptyIterator } +/** + * Returns a sequence of all elements from all sequences in this sequence. + */ +public fun Sequence>.flatten(): Sequence { + 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 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 +} + /** * A sequence that returns the values from the underlying [sequence] that either match or do not match * the specified [predicate].