From f596d9ac234ad98a01c23210ef3fe8c24dfc0050 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sun, 29 Nov 2015 05:04:31 +0300 Subject: [PATCH] Provide asIterable also for CharSequences, Maps and Iterables. KT-10152 Fixed --- libraries/stdlib/src/generated/_Arrays.kt | 171 +++++++++--------- .../stdlib/src/generated/_Collections.kt | 7 + libraries/stdlib/src/generated/_Maps.kt | 7 + libraries/stdlib/src/generated/_Sequences.kt | 9 + libraries/stdlib/src/generated/_Strings.kt | 10 + .../stdlib/src/kotlin/collections/Sequence.kt | 11 +- .../kotlin-stdlib-gen/src/templates/Arrays.kt | 12 -- .../src/templates/Sequence.kt | 21 +++ 8 files changed, 145 insertions(+), 103 deletions(-) diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index f6d9a2f8652..d7582dbdd38 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -5161,87 +5161,6 @@ public fun ShortArray.sortedWith(comparator: Comparator): List return toTypedArray().apply { sortWith(comparator) }.asList() } -/** - * Returns the Iterable that wraps the original array. - */ -public fun Array.asIterable(): Iterable { - return object : Iterable { - override fun iterator(): Iterator = this@asIterable.iterator() - } -} - -/** - * Returns the Iterable that wraps the original array. - */ -public fun BooleanArray.asIterable(): Iterable { - return object : Iterable { - override fun iterator(): Iterator = this@asIterable.iterator() - } -} - -/** - * Returns the Iterable that wraps the original array. - */ -public fun ByteArray.asIterable(): Iterable { - return object : Iterable { - override fun iterator(): Iterator = this@asIterable.iterator() - } -} - -/** - * Returns the Iterable that wraps the original array. - */ -public fun CharArray.asIterable(): Iterable { - return object : Iterable { - override fun iterator(): Iterator = this@asIterable.iterator() - } -} - -/** - * Returns the Iterable that wraps the original array. - */ -public fun DoubleArray.asIterable(): Iterable { - return object : Iterable { - override fun iterator(): Iterator = this@asIterable.iterator() - } -} - -/** - * Returns the Iterable that wraps the original array. - */ -public fun FloatArray.asIterable(): Iterable { - return object : Iterable { - override fun iterator(): Iterator = this@asIterable.iterator() - } -} - -/** - * Returns the Iterable that wraps the original array. - */ -public fun IntArray.asIterable(): Iterable { - return object : Iterable { - override fun iterator(): Iterator = this@asIterable.iterator() - } -} - -/** - * Returns the Iterable that wraps the original array. - */ -public fun LongArray.asIterable(): Iterable { - return object : Iterable { - override fun iterator(): Iterator = this@asIterable.iterator() - } -} - -/** - * Returns the Iterable that wraps the original array. - */ -public fun ShortArray.asIterable(): Iterable { - return object : Iterable { - override fun iterator(): Iterator = this@asIterable.iterator() - } -} - /** * Returns the range of valid indices for the array. */ @@ -10496,6 +10415,96 @@ public fun ShortArray.joinToString(separator: String = ", ", prefix: String = "" return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() } +/** + * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. + */ +public fun Array.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. + */ +public fun BooleanArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. + */ +public fun ByteArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. + */ +public fun CharArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. + */ +public fun DoubleArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. + */ +public fun FloatArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. + */ +public fun IntArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. + */ +public fun LongArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. + */ +public fun ShortArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + /** * Returns a sequence from the given collection. */ diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index bfb0f881881..1d1ed3151b5 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -1761,6 +1761,13 @@ public fun Iterable.joinToString(separator: String = ", ", prefix: String return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() } +/** + * Returns this collection as an [Iterable]. + */ +public fun Iterable.asIterable(): Iterable { + return this +} + /** * Returns a sequence from the given collection. */ diff --git a/libraries/stdlib/src/generated/_Maps.kt b/libraries/stdlib/src/generated/_Maps.kt index 5ccbf6f8d45..545f1c65576 100644 --- a/libraries/stdlib/src/generated/_Maps.kt +++ b/libraries/stdlib/src/generated/_Maps.kt @@ -188,6 +188,13 @@ public inline fun Map.none(predicate: (Map.Entry) -> Boolean) return true } +/** + * Creates an [Iterable] instance that wraps the original map returning its entrys when being iterated. + */ +public fun Map.asIterable(): Iterable> { + return entries +} + /** * Returns a sequence from the given collection. */ diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 41d7466a0f0..d6f0568f978 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -1075,6 +1075,15 @@ public fun Sequence.joinToString(separator: String = ", ", prefix: String return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() } +/** + * Creates an [Iterable] instance that wraps the original sequence returning its elements when being iterated. + */ +public fun Sequence.asIterable(): Iterable { + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + /** * Returns a sequence from the given collection. */ diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index d2cd1fd840e..cd64474447c 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -1607,6 +1607,16 @@ public infix fun String.zip(other: String): List> { return zip(other) { c1, c2 -> c1 to c2 } } +/** + * Creates an [Iterable] instance that wraps the original char sequence returning its characters when being iterated. + */ +public fun CharSequence.asIterable(): Iterable { + if (this is String && isEmpty()) return emptyList() + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + /** * Returns a sequence from the given collection. */ diff --git a/libraries/stdlib/src/kotlin/collections/Sequence.kt b/libraries/stdlib/src/kotlin/collections/Sequence.kt index 6e3e560c12f..578321e2580 100644 --- a/libraries/stdlib/src/kotlin/collections/Sequence.kt +++ b/libraries/stdlib/src/kotlin/collections/Sequence.kt @@ -20,7 +20,7 @@ public interface Sequence { } /** - * Creates a sequence that returns all values from this iterator. The sequence is constrained to be iterated only once. + * Creates a sequence that returns all elements from this iterator. The sequence is constrained to be iterated only once. */ public fun Iterator.asSequence(): Sequence { val iteratorSequence = object : Sequence { @@ -47,15 +47,6 @@ public fun sequenceOf(progression: Progression): Sequence = obje override fun iterator(): Iterator = progression.iterator() } -/** - * Returns an [Iterable] instance that wraps the original sequence. - */ -public fun Sequence.asIterable(): Iterable { - return object : Iterable { - override fun iterator(): Iterator = this@asIterable.iterator() - } -} - /** * Returns an empty sequence. */ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index d2b2d6f7e25..3a9084d811d 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -23,18 +23,6 @@ fun arrays(): List { } } - templates add f("asIterable()") { - only(ArraysOfObjects, ArraysOfPrimitives) - doc { "Returns the Iterable that wraps the original array." } - returns("Iterable") - body { - """ - return object : Iterable { - override fun iterator(): Iterator = this@asIterable.iterator() - } - """ - } - } templates add pval("lastIndex") { only(ArraysOfObjects, ArraysOfPrimitives) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Sequence.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Sequence.kt index 38f03c0e70e..2d8368d96f5 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Sequence.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Sequence.kt @@ -5,6 +5,27 @@ import templates.Family.* fun sequences(): List { val templates = arrayListOf() + templates add f("asIterable()") { + only(Iterables, ArraysOfObjects, ArraysOfPrimitives, Sequences, CharSequences, Maps) + doc { f -> "Creates an [Iterable] instance that wraps the original ${f.collection} returning its ${f.element}s when being iterated." } + returns("Iterable") + body { f -> + """ + ${ when(f) { + ArraysOfObjects, ArraysOfPrimitives -> "if (isEmpty()) return emptyList()" + CharSequences -> "if (this is String && isEmpty()) return emptyList()" + else -> "" + }} + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } + """ + } + doc(Iterables) { "Returns this collection as an [Iterable]." } + body(Iterables) { "return this" } + body(Maps) { "return entries" } + } + templates add f("asSequence()") { include(Maps) doc { "Returns a sequence from the given collection." }