is this `for (i in 1 until array.size)` the best...
# getting-started
o
is this
for (i in 1 until array.size)
the best equivalent to this?
for num in array[1:]
r
what kind of "best" function are we using here? readability? performance? minimum code length?
o
literal translation, the sample below is python
what I mean is that is there that kind of subscript kind of thing where you can say something like
1:
to mean
from 1 until the end
r
there is an
drop(1)
function, which copies whole iterable except the first x members (unless it's a sequence, in sequence it works 👌)
so for example
for (num in array.drop(1))
would work, but it would impact performance a bit, against the python version
o
ok so the one I suggested in the beginning i guess is the most readable
literally speaking
r
or you could turn into functional style:
Copy code
array.asSequence()
    .drop(1)
    .map { ... }
    .forEach { ... }
doesn't play well with nested for cycles, which are hella common in python tho refactoring them into functions helps tho
k
Re:
Copy code
ok so the one I suggested in the beginning i guess is the most readable
I think
for (num in array.drop(1))
is more readable because you can use
num
directly for each element, instead of
array[i]
. But it's a matter of opinion; do what's more readable for you and your colleagues or whoever reads your code.
p
Copy code
val array = intArrayOf(1, 2, 3)
array.sliceArray(1 until array.size)
on the JVM at least, sticking with arrays instead of dropping to sequences or lists is going to be a massive performance gain
👍🏻 1
o
thanks