2

Kotlin newbie here --

Is there a "neater" way to do this? (my int values are guaranteed unique across the entire set)

fun main() {
    val sect = arrayOf(arrayOf(1, 2, 3), arrayOf(4, 5), arrayOf(6, 7, 8, 9))
    val (i1, i2) = deepIndexOf(sect, 6)
    println("$i1 - $i2")
}
fun deepIndexOf(a: Array<Array<Int>>, x: Int): Pair<Int, Int> {
    for ((i1, sub) in a.withIndex()) {
        val i2 = sub.indexOf(x)
        if (i2 > -1) return i1 to i2
    }
    return -1 to -1
}

2 Answers 2

1
fun deepIndexOf(a: Array<Array<Int>>, x: Int): Pair<Int, Int> {
  return a
    .mapIndexed { index, ints -> index to ints.indexOf(x) }
    .firstOrNull { (_, found) -> found != -1 }
    ?: (-1 to -1)
}

Edit: based on @Alex.T's comment below, here is a solution which does not process the entire input list:

fun deepIndexOf(a: Array<Array<Int>>, x: Int): Pair<Int, Int> {
  return a
    .withIndex()
    .dropWhile { (_, ints) -> ints.indexOf(x) == -1 }
    .map { (index, ints) -> index to ints.indexOf(x) }
    .getOrElse(0) { -1 to -1 }
}
Sign up to request clarification or add additional context in comments.

2 Comments

wouldn't this process all of the sublists even if the sought element is in the first one? So a list of 1000 sublists would process 999 lists without any reason? Basically making the search a worst case scenario in all instances, no matter the length of the list?
@Alex.T you are of course correct. I just added a second solution addressing this.
1

One thing that stands out directly is that you could create an extension function. It would look something like this:

fun Array<Array<Int>>.deepIndexOf(element: Int): Pair<Int, Int> {
    for ((i1, this) in a.withIndex()) 
             ...

Then the call would look a bit more "natural", kind of like the call to sub.indexOf() looks like:

val (i1, i2) = sect.deepIndexOf(6)

One thing to note, if you would use List instead of Array, then you could do fun Collection<Collection<Int>.etc. Then you would be able to call this function on Set as well. Heck, you could call it on a setOf(listOf(), setOf(), listOf()).

Another change might be to use Kotlin's forEach, making the first lines:

fun Array<Array<Int>>.deepIndexOf(element: Int): Pair<Int, Int> {
    withIndex().forEach { (i1, sub) ->
              ...

As for the if statement, it looks ok as it is, you might be able to change it to something a bit more functional, but it might look forced, since it doesn't really need it. We can use takeIf instead of the if, which would give us the input value if the predicate is true, or null. Then we can use ?.let as an "if" statement to return. But honestly that creates more steps.

withIndex().forEach { (i1, sub) ->
    sub.indexOf(element) //get the index
        .takeIf { it > -1 } //if index > -1 propagate the index, else null
        ?.let { return i1 to it } //if the above is not null, return
}

To achieve the same number of steps as your example, we would need to remove the ?.let and that would force us to create something that is really hard to read.

withIndex().forEach { (i1, sub) ->
    return i1 to (sub.indexOf(element).takeIf { it > -1 } ?: return@forEach)
}

This uses the elvis operator ?: to say that if the result of sub.indexOf(element).takeIf { it > -1 } is null, then continue with the next element of the loop.

4 Comments

I'm gonna have to reread several more times for all this to sink in but impressed by (a) the quality of the comments and (b) how quickly you whipped all this up! Thank you
Note that the official coding conventions say that a for loop is preferable to forEach unless you’re using it in a null-safe call or at the end of a chain of functional calls.
@Tenfour04 True, you are right, I actually wanted to show the use of forEachIndexed, but somehow forgot to do it and ended up with withIndex.forEach.
I think the same convention would mean for (x in y.withIndex()) would be preferable to forEachIndexed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.