Provide asIterable also for CharSequences, Maps and Iterables.
KT-10152 Fixed
This commit is contained in:
@@ -5161,87 +5161,6 @@ public fun ShortArray.sortedWith(comparator: Comparator<in Short>): List<Short>
|
|||||||
return toTypedArray().apply { sortWith(comparator) }.asList()
|
return toTypedArray().apply { sortWith(comparator) }.asList()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Iterable that wraps the original array.
|
|
||||||
*/
|
|
||||||
public fun <T> Array<out T>.asIterable(): Iterable<T> {
|
|
||||||
return object : Iterable<T> {
|
|
||||||
override fun iterator(): Iterator<T> = this@asIterable.iterator()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Iterable that wraps the original array.
|
|
||||||
*/
|
|
||||||
public fun BooleanArray.asIterable(): Iterable<Boolean> {
|
|
||||||
return object : Iterable<Boolean> {
|
|
||||||
override fun iterator(): Iterator<Boolean> = this@asIterable.iterator()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Iterable that wraps the original array.
|
|
||||||
*/
|
|
||||||
public fun ByteArray.asIterable(): Iterable<Byte> {
|
|
||||||
return object : Iterable<Byte> {
|
|
||||||
override fun iterator(): Iterator<Byte> = this@asIterable.iterator()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Iterable that wraps the original array.
|
|
||||||
*/
|
|
||||||
public fun CharArray.asIterable(): Iterable<Char> {
|
|
||||||
return object : Iterable<Char> {
|
|
||||||
override fun iterator(): Iterator<Char> = this@asIterable.iterator()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Iterable that wraps the original array.
|
|
||||||
*/
|
|
||||||
public fun DoubleArray.asIterable(): Iterable<Double> {
|
|
||||||
return object : Iterable<Double> {
|
|
||||||
override fun iterator(): Iterator<Double> = this@asIterable.iterator()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Iterable that wraps the original array.
|
|
||||||
*/
|
|
||||||
public fun FloatArray.asIterable(): Iterable<Float> {
|
|
||||||
return object : Iterable<Float> {
|
|
||||||
override fun iterator(): Iterator<Float> = this@asIterable.iterator()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Iterable that wraps the original array.
|
|
||||||
*/
|
|
||||||
public fun IntArray.asIterable(): Iterable<Int> {
|
|
||||||
return object : Iterable<Int> {
|
|
||||||
override fun iterator(): Iterator<Int> = this@asIterable.iterator()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Iterable that wraps the original array.
|
|
||||||
*/
|
|
||||||
public fun LongArray.asIterable(): Iterable<Long> {
|
|
||||||
return object : Iterable<Long> {
|
|
||||||
override fun iterator(): Iterator<Long> = this@asIterable.iterator()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Iterable that wraps the original array.
|
|
||||||
*/
|
|
||||||
public fun ShortArray.asIterable(): Iterable<Short> {
|
|
||||||
return object : Iterable<Short> {
|
|
||||||
override fun iterator(): Iterator<Short> = this@asIterable.iterator()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the range of valid indices for the array.
|
* 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()
|
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 <T> Array<out T>.asIterable(): Iterable<T> {
|
||||||
|
if (isEmpty()) return emptyList()
|
||||||
|
return object : Iterable<T> {
|
||||||
|
override fun iterator(): Iterator<T> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an [Iterable] instance that wraps the original array returning its elements when being iterated.
|
||||||
|
*/
|
||||||
|
public fun BooleanArray.asIterable(): Iterable<Boolean> {
|
||||||
|
if (isEmpty()) return emptyList()
|
||||||
|
return object : Iterable<Boolean> {
|
||||||
|
override fun iterator(): Iterator<Boolean> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an [Iterable] instance that wraps the original array returning its elements when being iterated.
|
||||||
|
*/
|
||||||
|
public fun ByteArray.asIterable(): Iterable<Byte> {
|
||||||
|
if (isEmpty()) return emptyList()
|
||||||
|
return object : Iterable<Byte> {
|
||||||
|
override fun iterator(): Iterator<Byte> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an [Iterable] instance that wraps the original array returning its elements when being iterated.
|
||||||
|
*/
|
||||||
|
public fun CharArray.asIterable(): Iterable<Char> {
|
||||||
|
if (isEmpty()) return emptyList()
|
||||||
|
return object : Iterable<Char> {
|
||||||
|
override fun iterator(): Iterator<Char> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an [Iterable] instance that wraps the original array returning its elements when being iterated.
|
||||||
|
*/
|
||||||
|
public fun DoubleArray.asIterable(): Iterable<Double> {
|
||||||
|
if (isEmpty()) return emptyList()
|
||||||
|
return object : Iterable<Double> {
|
||||||
|
override fun iterator(): Iterator<Double> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an [Iterable] instance that wraps the original array returning its elements when being iterated.
|
||||||
|
*/
|
||||||
|
public fun FloatArray.asIterable(): Iterable<Float> {
|
||||||
|
if (isEmpty()) return emptyList()
|
||||||
|
return object : Iterable<Float> {
|
||||||
|
override fun iterator(): Iterator<Float> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an [Iterable] instance that wraps the original array returning its elements when being iterated.
|
||||||
|
*/
|
||||||
|
public fun IntArray.asIterable(): Iterable<Int> {
|
||||||
|
if (isEmpty()) return emptyList()
|
||||||
|
return object : Iterable<Int> {
|
||||||
|
override fun iterator(): Iterator<Int> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an [Iterable] instance that wraps the original array returning its elements when being iterated.
|
||||||
|
*/
|
||||||
|
public fun LongArray.asIterable(): Iterable<Long> {
|
||||||
|
if (isEmpty()) return emptyList()
|
||||||
|
return object : Iterable<Long> {
|
||||||
|
override fun iterator(): Iterator<Long> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an [Iterable] instance that wraps the original array returning its elements when being iterated.
|
||||||
|
*/
|
||||||
|
public fun ShortArray.asIterable(): Iterable<Short> {
|
||||||
|
if (isEmpty()) return emptyList()
|
||||||
|
return object : Iterable<Short> {
|
||||||
|
override fun iterator(): Iterator<Short> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a sequence from the given collection.
|
* Returns a sequence from the given collection.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1761,6 +1761,13 @@ public fun <T> Iterable<T>.joinToString(separator: String = ", ", prefix: String
|
|||||||
return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString()
|
return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns this collection as an [Iterable].
|
||||||
|
*/
|
||||||
|
public fun <T> Iterable<T>.asIterable(): Iterable<T> {
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a sequence from the given collection.
|
* Returns a sequence from the given collection.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -188,6 +188,13 @@ public inline fun <K, V> Map<K, V>.none(predicate: (Map.Entry<K, V>) -> Boolean)
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an [Iterable] instance that wraps the original map returning its entrys when being iterated.
|
||||||
|
*/
|
||||||
|
public fun <K, V> Map<K, V>.asIterable(): Iterable<Map.Entry<K, V>> {
|
||||||
|
return entries
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a sequence from the given collection.
|
* Returns a sequence from the given collection.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1075,6 +1075,15 @@ public fun <T> Sequence<T>.joinToString(separator: String = ", ", prefix: String
|
|||||||
return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString()
|
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 <T> Sequence<T>.asIterable(): Iterable<T> {
|
||||||
|
return object : Iterable<T> {
|
||||||
|
override fun iterator(): Iterator<T> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a sequence from the given collection.
|
* Returns a sequence from the given collection.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1607,6 +1607,16 @@ public infix fun String.zip(other: String): List<Pair<Char, Char>> {
|
|||||||
return zip(other) { c1, c2 -> c1 to c2 }
|
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<Char> {
|
||||||
|
if (this is String && isEmpty()) return emptyList()
|
||||||
|
return object : Iterable<Char> {
|
||||||
|
override fun iterator(): Iterator<Char> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a sequence from the given collection.
|
* Returns a sequence from the given collection.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public interface Sequence<out T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 <T> Iterator<T>.asSequence(): Sequence<T> {
|
public fun <T> Iterator<T>.asSequence(): Sequence<T> {
|
||||||
val iteratorSequence = object : Sequence<T> {
|
val iteratorSequence = object : Sequence<T> {
|
||||||
@@ -47,15 +47,6 @@ public fun <T : Any> sequenceOf(progression: Progression<T>): Sequence<T> = obje
|
|||||||
override fun iterator(): Iterator<T> = progression.iterator()
|
override fun iterator(): Iterator<T> = progression.iterator()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an [Iterable] instance that wraps the original sequence.
|
|
||||||
*/
|
|
||||||
public fun <T> Sequence<T>.asIterable(): Iterable<T> {
|
|
||||||
return object : Iterable<T> {
|
|
||||||
override fun iterator(): Iterator<T> = this@asIterable.iterator()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an empty sequence.
|
* Returns an empty sequence.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -23,18 +23,6 @@ fun arrays(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("asIterable()") {
|
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
|
||||||
doc { "Returns the Iterable that wraps the original array." }
|
|
||||||
returns("Iterable<T>")
|
|
||||||
body {
|
|
||||||
"""
|
|
||||||
return object : Iterable<T> {
|
|
||||||
override fun iterator(): Iterator<T> = this@asIterable.iterator()
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
templates add pval("lastIndex") {
|
templates add pval("lastIndex") {
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
|||||||
@@ -5,6 +5,27 @@ import templates.Family.*
|
|||||||
fun sequences(): List<GenericFunction> {
|
fun sequences(): List<GenericFunction> {
|
||||||
val templates = arrayListOf<GenericFunction>()
|
val templates = arrayListOf<GenericFunction>()
|
||||||
|
|
||||||
|
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<T>")
|
||||||
|
body { f ->
|
||||||
|
"""
|
||||||
|
${ when(f) {
|
||||||
|
ArraysOfObjects, ArraysOfPrimitives -> "if (isEmpty()) return emptyList()"
|
||||||
|
CharSequences -> "if (this is String && isEmpty()) return emptyList()"
|
||||||
|
else -> ""
|
||||||
|
}}
|
||||||
|
return object : Iterable<T> {
|
||||||
|
override fun iterator(): Iterator<T> = this@asIterable.iterator()
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
doc(Iterables) { "Returns this collection as an [Iterable]." }
|
||||||
|
body(Iterables) { "return this" }
|
||||||
|
body(Maps) { "return entries" }
|
||||||
|
}
|
||||||
|
|
||||||
templates add f("asSequence()") {
|
templates add f("asSequence()") {
|
||||||
include(Maps)
|
include(Maps)
|
||||||
doc { "Returns a sequence from the given collection." }
|
doc { "Returns a sequence from the given collection." }
|
||||||
|
|||||||
Reference in New Issue
Block a user