Provide asIterable also for CharSequences, Maps and Iterables.

KT-10152 Fixed
This commit is contained in:
Ilya Gorbunov
2015-11-29 05:04:31 +03:00
parent 45db5db7e8
commit f596d9ac23
8 changed files with 145 additions and 103 deletions
@@ -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") {
only(ArraysOfObjects, ArraysOfPrimitives)
@@ -5,6 +5,27 @@ import templates.Family.*
fun sequences(): List<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()") {
include(Maps)
doc { "Returns a sequence from the given collection." }