From db93532e7c676179d36a8419b78b51eb6e89d9c7 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 7 Oct 2015 04:42:59 +0300 Subject: [PATCH] Deprecate merge and introduce instead zip with transform. Add zip with transform for Strings. --- libraries/stdlib/src/generated/_Arrays.kt | 686 +++++++++++------- .../stdlib/src/generated/_Collections.kt | 56 +- libraries/stdlib/src/generated/_Sequences.kt | 13 +- libraries/stdlib/src/generated/_Strings.kt | 16 +- .../stdlib/test/collections/CollectionTest.kt | 4 +- .../stdlib/test/collections/SequenceTest.kt | 4 +- .../kotlin-stdlib-gen/src/templates/Engine.kt | 2 + .../src/templates/Generators.kt | 81 ++- 8 files changed, 536 insertions(+), 326 deletions(-) diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index b89ae9c7a7a..24734087b76 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -8942,334 +8942,134 @@ public fun Array.requireNoNulls(): Array { return this as Array } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun Array.merge(array: Array, transform: (T, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun BooleanArray.merge(array: Array, transform: (Boolean, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun ByteArray.merge(array: Array, transform: (Byte, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun CharArray.merge(array: Array, transform: (Char, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun DoubleArray.merge(array: Array, transform: (Double, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun FloatArray.merge(array: Array, transform: (Float, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun IntArray.merge(array: Array, transform: (Int, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun LongArray.merge(array: Array, transform: (Long, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun ShortArray.merge(array: Array, transform: (Short, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun BooleanArray.merge(array: BooleanArray, transform: (Boolean, Boolean) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun ByteArray.merge(array: ByteArray, transform: (Byte, Byte) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun CharArray.merge(array: CharArray, transform: (Char, Char) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun DoubleArray.merge(array: DoubleArray, transform: (Double, Double) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun FloatArray.merge(array: FloatArray, transform: (Float, Float) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun IntArray.merge(array: IntArray, transform: (Int, Int) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun LongArray.merge(array: LongArray, transform: (Long, Long) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun ShortArray.merge(array: ShortArray, transform: (Short, Short) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(other, transform)")) public inline fun Array.merge(other: Iterable, transform: (T, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list + return zip(other, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(other, transform)")) public inline fun BooleanArray.merge(other: Iterable, transform: (Boolean, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list + return zip(other, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(other, transform)")) public inline fun ByteArray.merge(other: Iterable, transform: (Byte, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list + return zip(other, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(other, transform)")) public inline fun CharArray.merge(other: Iterable, transform: (Char, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list + return zip(other, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(other, transform)")) public inline fun DoubleArray.merge(other: Iterable, transform: (Double, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list + return zip(other, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(other, transform)")) public inline fun FloatArray.merge(other: Iterable, transform: (Float, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list + return zip(other, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(other, transform)")) public inline fun IntArray.merge(other: Iterable, transform: (Int, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list + return zip(other, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(other, transform)")) public inline fun LongArray.merge(other: Iterable, transform: (Long, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list + return zip(other, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(other, transform)")) public inline fun ShortArray.merge(other: Iterable, transform: (Short, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list + return zip(other, transform) } /** @@ -9438,182 +9238,512 @@ public inline fun ShortArray.partition(predicate: (Short) -> Boolean): Pair Array.zip(array: Array): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun BooleanArray.zip(array: Array): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun ByteArray.zip(array: Array): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun CharArray.zip(array: Array): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun DoubleArray.zip(array: Array): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun FloatArray.zip(array: Array): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun IntArray.zip(array: Array): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun LongArray.zip(array: Array): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun ShortArray.zip(array: Array): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun Array.zip(array: Array, transform: (T, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun BooleanArray.zip(array: Array, transform: (Boolean, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun ByteArray.zip(array: Array, transform: (Byte, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun CharArray.zip(array: Array, transform: (Char, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun DoubleArray.zip(array: Array, transform: (Double, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun FloatArray.zip(array: Array, transform: (Float, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun IntArray.zip(array: Array, transform: (Int, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun LongArray.zip(array: Array, transform: (Long, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun ShortArray.zip(array: Array, transform: (Short, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun BooleanArray.zip(array: BooleanArray): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun ByteArray.zip(array: ByteArray): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun CharArray.zip(array: CharArray): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun DoubleArray.zip(array: DoubleArray): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun FloatArray.zip(array: FloatArray): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun IntArray.zip(array: IntArray): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun LongArray.zip(array: LongArray): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun ShortArray.zip(array: ShortArray): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun BooleanArray.zip(array: BooleanArray, transform: (Boolean, Boolean) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun ByteArray.zip(array: ByteArray, transform: (Byte, Byte) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun CharArray.zip(array: CharArray, transform: (Char, Char) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun DoubleArray.zip(array: DoubleArray, transform: (Double, Double) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun FloatArray.zip(array: FloatArray, transform: (Float, Float) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun IntArray.zip(array: IntArray, transform: (Int, Int) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun LongArray.zip(array: LongArray, transform: (Long, Long) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun ShortArray.zip(array: ShortArray, transform: (Short, Short) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun Array.zip(other: Iterable): List> { - return merge(other) { t1, t2 -> t1 to t2 } + return zip(other) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun BooleanArray.zip(other: Iterable): List> { - return merge(other) { t1, t2 -> t1 to t2 } + return zip(other) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun ByteArray.zip(other: Iterable): List> { - return merge(other) { t1, t2 -> t1 to t2 } + return zip(other) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun CharArray.zip(other: Iterable): List> { - return merge(other) { t1, t2 -> t1 to t2 } + return zip(other) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun DoubleArray.zip(other: Iterable): List> { - return merge(other) { t1, t2 -> t1 to t2 } + return zip(other) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun FloatArray.zip(other: Iterable): List> { - return merge(other) { t1, t2 -> t1 to t2 } + return zip(other) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun IntArray.zip(other: Iterable): List> { - return merge(other) { t1, t2 -> t1 to t2 } + return zip(other) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun LongArray.zip(other: Iterable): List> { - return merge(other) { t1, t2 -> t1 to t2 } + return zip(other) { t1, t2 -> t1 to t2 } } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun ShortArray.zip(other: Iterable): List> { - return merge(other) { t1, t2 -> t1 to t2 } + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun Array.zip(other: Iterable, transform: (T, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun BooleanArray.zip(other: Iterable, transform: (Boolean, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun ByteArray.zip(other: Iterable, transform: (Byte, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun CharArray.zip(other: Iterable, transform: (Char, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun DoubleArray.zip(other: Iterable, transform: (Double, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun FloatArray.zip(other: Iterable, transform: (Float, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun IntArray.zip(other: Iterable, transform: (Int, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun LongArray.zip(other: Iterable, transform: (Long, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun ShortArray.zip(other: Iterable, transform: (Short, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list } /** diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 1a6f1b7d3f9..15c75593125 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -1334,31 +1334,14 @@ public fun List.requireNoNulls(): List { return this as List } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(array, transform)")) public inline fun Iterable.merge(array: Array, transform: (T, R) -> V): List { - val arraySize = array.size() - val list = ArrayList(Math.min(collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in this) { - if (i >= arraySize) break - list.add(transform(element, array[i++])) - } - return list + return zip(array, transform) } -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(other, transform)")) public inline fun Iterable.merge(other: Iterable, transform: (T, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(Math.min(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) - } - return list + return zip(other, transform) } /** @@ -1510,14 +1493,41 @@ public operator fun Iterable.plus(sequence: Sequence): List { * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun Iterable.zip(array: Array): List> { - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun Iterable.zip(array: Array, transform: (T, R) -> V): List { + val arraySize = array.size() + val list = ArrayList(Math.min(collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in this) { + if (i >= arraySize) break + list.add(transform(element, array[i++])) + } + return list } /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ public fun Iterable.zip(other: Iterable): List> { - return merge(other) { t1, t2 -> t1 to t2 } + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun Iterable.zip(other: Iterable, transform: (T, R) -> V): List { + val first = iterator() + val second = other.iterator() + val list = ArrayList(Math.min(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) + while (first.hasNext() && second.hasNext()) { + list.add(transform(first.next(), second.next())) + } + return list } /** diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 732947f7aa5..2e357944996 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -795,11 +795,9 @@ public fun Sequence.requireNoNulls(): Sequence { return map { it ?: throw IllegalArgumentException("null element found in $this.") } } -/** - * Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. - */ +@Deprecated("Use zip() with transform instead.", ReplaceWith("zip(sequence, transform)")) public fun Sequence.merge(sequence: Sequence, transform: (T, R) -> V): Sequence { - return MergingSequence(this, sequence, transform) + return zip(sequence, transform) } /** @@ -923,6 +921,13 @@ public fun Sequence.zip(sequence: Sequence): Sequence> { return MergingSequence(this, sequence) { t1, t2 -> t1 to t2 } } +/** + * Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. + */ +public fun Sequence.zip(sequence: Sequence, transform: (T, R) -> V): Sequence { + return MergingSequence(this, sequence, transform) +} + /** * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index d9778043d63..6ef7a9df5df 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -733,11 +733,17 @@ public inline fun String.partition(predicate: (Char) -> Boolean): Pair> { - val first = iterator() - val second = other.iterator() - val list = ArrayList>(length()) - while (first.hasNext() && second.hasNext()) { - list.add(first.next() to second.next()) + return zip(other) { c1, c2 -> c1 to c2 } +} + +/** + * Returns a list of values built from characters of both strings with same indexes using provided [transform]. List has length of shortest string. + */ +public inline fun String.zip(other: String, transform: (Char, Char) -> V): List { + val length = Math.min(this.length(), other.length()) + val list = ArrayList(length) + for (i in 0..length-1) { + list.add(transform(this[i], other[i])) } return list } diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index e6e28cc4e78..7179efac19d 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -134,9 +134,9 @@ class CollectionTest { } @test - fun merge() { + fun zipTransform() { expect(listOf("ab", "bc", "cd")) { - listOf("a", "b", "c").merge(listOf("b", "c", "d")) { a, b -> a + b } + listOf("a", "b", "c").zip(listOf("b", "c", "d")) { a, b -> a + b } } } diff --git a/libraries/stdlib/test/collections/SequenceTest.kt b/libraries/stdlib/test/collections/SequenceTest.kt index 2d7baa14185..25524b05c06 100644 --- a/libraries/stdlib/test/collections/SequenceTest.kt +++ b/libraries/stdlib/test/collections/SequenceTest.kt @@ -120,9 +120,9 @@ public class SequenceTest { assertEquals("", sequenceOf(1).dropWhile { it < 200 }.joinToString(limit = 10)) } - @test fun merge() { + @test fun zip() { expect(listOf("ab", "bc", "cd")) { - sequenceOf("a", "b", "c").merge(sequenceOf("b", "c", "d")) { a, b -> a + b }.toList() + sequenceOf("a", "b", "c").zip(sequenceOf("b", "c", "d")) { a, b -> a + b }.toList() } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt index b13aeaa182f..9b337b8740e 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt @@ -65,6 +65,8 @@ class GenericFunction(val signature: String, val keyword: String = "fun") { open class SpecializedProperty() { private val values = HashMap() + val default: TValue? get() = values.get(null) + operator fun get(key: TKey): TValue? = values.getOrElse(key, { values.getOrElse(null, { null }) }) operator fun set(keys: Collection, value: TValue) { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index 2070d0f39cd..7de3f78bd8e 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -444,6 +444,17 @@ fun generators(): List { } templates add f("merge(other: Iterable, transform: (T, R) -> V)") { + exclude(Sequences, Strings) + typeParam("R") + typeParam("V") + returns("List") + inline(true) + deprecate("Use zip() with transform instead.") + deprecateReplacement("zip(other, transform)") + body { """return ${deprecateReplacement.default}""" } + } + + templates add f("zip(other: Iterable, transform: (T, R) -> V)") { exclude(Sequences, Strings) doc { """ @@ -480,6 +491,17 @@ fun generators(): List { } templates add f("merge(array: Array, transform: (T, R) -> V)") { + exclude(Sequences, Strings) + typeParam("R") + typeParam("V") + returns("List") + inline(true) + deprecate("Use zip() with transform instead.") + deprecateReplacement("zip(array, transform)") + body { """return ${deprecateReplacement.default}""" } + } + + templates add f("zip(array: Array, transform: (T, R) -> V)") { exclude(Sequences, Strings) doc { """ @@ -515,8 +537,17 @@ fun generators(): List { } - templates add f("merge(array: SELF, transform: (T, T) -> V)") { + only(ArraysOfPrimitives) + typeParam("V") + returns("List") + inline(true) + deprecate("Use zip() with transform instead.") + deprecateReplacement("zip(array, transform)") + body { """return ${deprecateReplacement.default}""" } + } + + templates add f("zip(array: SELF, transform: (T, T) -> V)") { only(ArraysOfPrimitives) doc { """ @@ -538,8 +569,17 @@ fun generators(): List { } } - templates add f("merge(sequence: Sequence, transform: (T, R) -> V)") { + only(Sequences) + typeParam("R") + typeParam("V") + returns("Sequence") + deprecate("Use zip() with transform instead.") + deprecateReplacement("zip(sequence, transform)") + body { """return ${deprecateReplacement.default}""" } + } + + templates add f("zip(sequence: Sequence, transform: (T, R) -> V)") { only(Sequences) doc { """ @@ -556,6 +596,29 @@ fun generators(): List { } } + templates add f("zip(other: String, transform: (Char, Char) -> V)") { + only(Strings) + doc { + """ + Returns a list of values built from characters of both strings with same indexes using provided [transform]. List has length of shortest string. + """ + } + typeParam("V") + returns("List") + inline(true) + body { + """ + val length = Math.min(this.length(), other.length()) + + val list = ArrayList(length) + for (i in 0..length-1) { + list.add(transform(this[i], other[i])) + } + return list + """ + } + } + templates add f("zip(other: Iterable)") { exclude(Sequences, Strings) @@ -568,7 +631,7 @@ fun generators(): List { returns("List>") body { """ - return merge(other) { t1, t2 -> t1 to t2 } + return zip(other) { t1, t2 -> t1 to t2 } """ } } @@ -583,13 +646,7 @@ fun generators(): List { returns("List>") body { """ - val first = iterator() - val second = other.iterator() - val list = ArrayList>(length()) - while (first.hasNext() && second.hasNext()) { - list.add(first.next() to second.next()) - } - return list + return zip(other) { c1, c2 -> c1 to c2 } """ } } @@ -605,7 +662,7 @@ fun generators(): List { returns("List>") body { """ - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } """ } } @@ -620,7 +677,7 @@ fun generators(): List { returns("List>") body { """ - return merge(array) { t1, t2 -> t1 to t2 } + return zip(array) { t1, t2 -> t1 to t2 } """ } }