Delegate indexOf and lastIndexOf to List if the Iterable is List.

This commit is contained in:
Ilya Gorbunov
2015-10-15 19:23:42 +03:00
parent 45d13fafc8
commit c5cd599e30
2 changed files with 16 additions and 35 deletions
@@ -241,6 +241,7 @@ public fun <T> List<T>.getOrNull(index: Int): T? {
* Returns first index of [element], or -1 if the collection does not contain element. * Returns first index of [element], or -1 if the collection does not contain element.
*/ */
public fun <T> Iterable<T>.indexOf(element: T): Int { public fun <T> Iterable<T>.indexOf(element: T): Int {
if (this is List) return this.indexOf(element)
var index = 0 var index = 0
for (item in this) { for (item in this) {
if (element == item) if (element == item)
@@ -370,6 +371,7 @@ public inline fun <T> List<T>.last(predicate: (T) -> Boolean): T {
* Returns last index of [element], or -1 if the collection does not contain element. * Returns last index of [element], or -1 if the collection does not contain element.
*/ */
public fun <T> Iterable<T>.lastIndexOf(element: T): Int { public fun <T> Iterable<T>.lastIndexOf(element: T): Int {
if (this is List) return this.lastIndexOf(element)
var lastIndex = -1 var lastIndex = -1
var index = 0 var index = 0
for (item in this) { for (item in this) {
@@ -29,8 +29,9 @@ fun elements(): List<GenericFunction> {
exclude(Strings, Lists) // has native implementation exclude(Strings, Lists) // has native implementation
doc { "Returns first index of [element], or -1 if the collection does not contain element." } doc { "Returns first index of [element], or -1 if the collection does not contain element." }
returns("Int") returns("Int")
body { body { f ->
""" """
${if (f == Iterables) "if (this is List) return this.indexOf(element)" else ""}
var index = 0 var index = 0
for (item in this) { for (item in this) {
if (element == item) if (element == item)
@@ -75,8 +76,9 @@ fun elements(): List<GenericFunction> {
exclude(Strings, Lists) // has native implementation exclude(Strings, Lists) // has native implementation
doc { "Returns last index of [element], or -1 if the collection does not contain element." } doc { "Returns last index of [element], or -1 if the collection does not contain element." }
returns("Int") returns("Int")
body { body { f ->
""" """
${if (f == Iterables) "if (this is List) return this.lastIndexOf(element)" else ""}
var lastIndex = -1 var lastIndex = -1
var index = 0 var index = 0
for (item in this) { for (item in this) {
@@ -521,26 +523,14 @@ fun elements(): List<GenericFunction> {
doc(Strings) { """"Returns the last character matching the given [predicate]. doc(Strings) { """"Returns the last character matching the given [predicate].
@throws [NoSuchElementException] if no such character is found.""" } @throws [NoSuchElementException] if no such character is found.""" }
returns("T") returns("T")
body { body { f ->
""" (if (f == Iterables)
var last: T? = null
var found = false
for (element in this) {
if (predicate(element)) {
last = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
return last as T
"""
}
body(Iterables) {
""" """
if (this is List) if (this is List)
return this.last(predicate) return this.last(predicate)
"""
else "") +
"""
var last: T? = null var last: T? = null
var found = false var found = false
for (element in this) { for (element in this) {
@@ -570,23 +560,14 @@ fun elements(): List<GenericFunction> {
doc { "Returns the last element matching the given [predicate], or `null` if no such element was found." } doc { "Returns the last element matching the given [predicate], or `null` if no such element was found." }
doc(Strings) { "Returns the last character matching the given [predicate], or `null` if no such character was found." } doc(Strings) { "Returns the last character matching the given [predicate], or `null` if no such character was found." }
returns("T?") returns("T?")
body { body { f ->
""" (if (f == Iterables)
var last: T? = null
for (element in this) {
if (predicate(element)) {
last = element
}
}
return last
"""
}
body(Iterables) {
""" """
if (this is List) if (this is List)
return this.lastOrNull(predicate) return this.lastOrNull(predicate)
"""
else "") +
"""
var last: T? = null var last: T? = null
for (element in this) { for (element in this) {
if (predicate(element)) { if (predicate(element)) {
@@ -597,8 +578,6 @@ fun elements(): List<GenericFunction> {
""" """
} }
body(ArraysOfPrimitives, ArraysOfObjects, Lists) { body(ArraysOfPrimitives, ArraysOfObjects, Lists) {
""" """
for (index in this.indices.reversed()) { for (index in this.indices.reversed()) {