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
}
Thank you!
CodePudding user response:
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.