Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin has array.indexOf but I cannot figure out how to do array.indexOfBy { lambda }

Tags:

kotlin

Kotlin has array.indexOf(item) but I cannot figure out how to do array.indexOfBy { lambda }. Does it not exist? I can find an item, but I cannot get its index at the same time.

Am I missing a function in the stdlib?

I can create a function with a loop that chekcs the items and returns when it finds the target. Like this:

fun <T : Any> indexOfBy(items: Array<T>, predicate: (T) -> Boolean): Int {
    for (i in items.indices) { // or (i in 0..items.size-1)
        if (predicate(items[i])) {
            return i
        }
    }
    return -1
}

Then I tried to make it a little more functional using forEach:

fun <T : Any> indexOfBy(items: Array<T>, predicate: (T) -> Boolean): Int {
    (items.indices).forEach {
        if (predicate(items[it])) {
            return it
        }
    }
    return -1
}

Or I can do something silly like this which isn't very performant:

val slowAndSilly = people.indexOf(people.find { it.name == "David" })

And what looks best is maybe as extension functions:

fun <T: Any> Array<T>.indexOfBy(predicate: (T)->Boolean): Int =
        this.withIndex().find { predicate(it.value) }?.index ?: -1
fun <T: Any> Collection<T>.indexOfBy(predicate: (T)->Boolean): Int =
        this.withIndex().find { predicate(it.value) }?.index ?: -1
fun <T: Any> Sequence<T>.indexOfBy(predicate: (T)->Boolean): Int =
        this.withIndex().find { predicate(it.value) }?.index ?: -1

Is there a more elegant and idiomatic way to accomplish this?!? I also don't see a function like this for lists, collections nor sequences.

(this question is derived from the comment on another post)

like image 268
Jayson Minard Avatar asked Mar 02 '16 22:03

Jayson Minard


People also ask

How do you find the index of an array in Kotlin?

Using arrayOf() function In Kotlin, you can use the indexOf() function that returns the index of the first occurrence of the given element, or -1 if the array does not contain the element.

How do you use Kotlin indexOf?

Kotlin List – Find Index of an Element. To find the index of a specific element in this List, call indexOf() function on this List, and pass the element as argument to this indexOf() function. The Kotlin List. indexOf() function finds and returns the index of first occurrence of the element in the list.

How do you get the index of an element in a list in Kotlin?

In any lists, you can find the position of an element using the functions indexOf() and lastIndexOf() . They return the first and the last position of an element equal to the given argument in the list.

How do you find the index of a string in Kotlin?

To find index of substring in this string in Kotlin, call indexOf() method on this string, and pass the substring as argument. String. indexOf() returns an integer representing the index of first occurrence of the match for the given substring.


1 Answers

You can use indexOfFirst

arrayOf(1, 2, 3).indexOfFirst { it == 2 } // returns 1
arrayOf(4, 5, 6).indexOfFirst { it < 3 } // returns -1
like image 106
mfulton26 Avatar answered Oct 20 '22 12:10

mfulton26