Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
Nested array indexOf()

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
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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 }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda