Delegate indexOf and lastIndexOf to List if the Iterable is List.
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
public fun <T> Iterable<T>.indexOf(element: T): Int {
|
||||
if (this is List) return this.indexOf(element)
|
||||
var index = 0
|
||||
for (item in this) {
|
||||
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.
|
||||
*/
|
||||
public fun <T> Iterable<T>.lastIndexOf(element: T): Int {
|
||||
if (this is List) return this.lastIndexOf(element)
|
||||
var lastIndex = -1
|
||||
var index = 0
|
||||
for (item in this) {
|
||||
|
||||
@@ -29,8 +29,9 @@ fun elements(): List<GenericFunction> {
|
||||
exclude(Strings, Lists) // has native implementation
|
||||
doc { "Returns first index of [element], or -1 if the collection does not contain element." }
|
||||
returns("Int")
|
||||
body {
|
||||
body { f ->
|
||||
"""
|
||||
${if (f == Iterables) "if (this is List) return this.indexOf(element)" else ""}
|
||||
var index = 0
|
||||
for (item in this) {
|
||||
if (element == item)
|
||||
@@ -75,8 +76,9 @@ fun elements(): List<GenericFunction> {
|
||||
exclude(Strings, Lists) // has native implementation
|
||||
doc { "Returns last index of [element], or -1 if the collection does not contain element." }
|
||||
returns("Int")
|
||||
body {
|
||||
body { f ->
|
||||
"""
|
||||
${if (f == Iterables) "if (this is List) return this.lastIndexOf(element)" else ""}
|
||||
var lastIndex = -1
|
||||
var index = 0
|
||||
for (item in this) {
|
||||
@@ -521,26 +523,14 @@ fun elements(): List<GenericFunction> {
|
||||
doc(Strings) { """"Returns the last character matching the given [predicate].
|
||||
@throws [NoSuchElementException] if no such character is found.""" }
|
||||
returns("T")
|
||||
body {
|
||||
"""
|
||||
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) {
|
||||
body { f ->
|
||||
(if (f == Iterables)
|
||||
"""
|
||||
if (this is List)
|
||||
return this.last(predicate)
|
||||
|
||||
"""
|
||||
else "") +
|
||||
"""
|
||||
var last: T? = null
|
||||
var found = false
|
||||
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(Strings) { "Returns the last character matching the given [predicate], or `null` if no such character was found." }
|
||||
returns("T?")
|
||||
body {
|
||||
"""
|
||||
var last: T? = null
|
||||
for (element in this) {
|
||||
if (predicate(element)) {
|
||||
last = element
|
||||
}
|
||||
}
|
||||
return last
|
||||
"""
|
||||
}
|
||||
|
||||
body(Iterables) {
|
||||
body { f ->
|
||||
(if (f == Iterables)
|
||||
"""
|
||||
if (this is List)
|
||||
return this.lastOrNull(predicate)
|
||||
|
||||
"""
|
||||
else "") +
|
||||
"""
|
||||
var last: T? = null
|
||||
for (element in this) {
|
||||
if (predicate(element)) {
|
||||
@@ -597,8 +578,6 @@ fun elements(): List<GenericFunction> {
|
||||
"""
|
||||
}
|
||||
|
||||
|
||||
|
||||
body(ArraysOfPrimitives, ArraysOfObjects, Lists) {
|
||||
"""
|
||||
for (index in this.indices.reversed()) {
|
||||
|
||||
Reference in New Issue
Block a user