Deprecate withIndicies(), introduce optimized and fixed withIndex and forEachIndexed()

This commit is contained in:
Ilya Ryzhenkov
2014-11-06 20:35:33 +03:00
parent b5fb09b8bc
commit e890c2ee0d
8 changed files with 273 additions and 6 deletions
@@ -395,5 +395,17 @@ fun aggregates(): List<GenericFunction> {
include(Maps)
}
templates add f("forEachIndexed(operation: (Int, T) -> Unit)") {
inline(true)
doc { "Performs the given *operation* on each element, providing sequential index with the element" }
returns("Unit")
body {
"""
var index = 0
for (item in this) operation(index++, item)
"""
}
}
return templates
}
@@ -6,6 +6,7 @@ fun mapping(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("withIndices()") {
deprecate { "Use withIndex() instead." }
doc { "Returns a list containing pairs of each element of the original collection and their index" }
returns("List<Pair<Int, T>>")
body {
@@ -25,6 +26,24 @@ fun mapping(): List<GenericFunction> {
}
}
templates add f("withIndex()") {
doc { "Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection" }
returns("Iterable<IndexedValue<T>>")
body {
"""
return IndexingIterable { iterator() }
"""
}
returns(Streams) { "Stream<IndexedValue<T>>" }
doc(Streams) { "Returns a stream of [IndexedValue] for each element of the original stream" }
body(Streams) {
"""
return IndexingStream(this)
"""
}
}
templates add f("map(transform: (T) -> R)") {
inline(true)