https://kotlinlang.org logo
Title
o

oday

08/01/2017, 10:43 AM
is there a cool new flashy kotlin way to iterate over 2 lists of the same size in Kotlin?
k

kingsley

08/01/2017, 10:44 AM
zip
?
o

oday

08/01/2017, 10:44 AM
zip…ok I’ll look into it, makes them into 1 list that you can iterate over?
m

menegatti

08/01/2017, 10:45 AM
map elements at the same index to a pair and then you can iterate as usual
o

oday

08/01/2017, 10:46 AM
got it
but how do I pick first or second
to access the properties of the objects? isnt first referring to the first of the pair and second to the second>
m

menegatti

08/01/2017, 10:46 AM
a pair has
first
and
second
attributes
o

oday

08/01/2017, 10:46 AM
yes
k

kingsley

08/01/2017, 10:47 AM
Or you could destructure. Something like:
val a = listOf("a", "b")
    val b = listOf("c", "d")
    a.zip(b).forEach { (first, second) -> print(first + second) }
Whatever works for you man
o

oday

08/01/2017, 10:48 AM
I add “Pipe” object which has these params, and i use those pipes to draw them with a drawRect
allPipes.forEach {
      canvas?.drawRect(it.first.distanceX, it.distanceY, it.width,
          it.height, painter)
    }
do I have to have now two drawRects? one including `first`‘s params and the other including `second`‘s params?
m

menegatti

08/01/2017, 10:49 AM
I don't follow
o

oday

08/01/2017, 10:50 AM
it’s a class
Pipe
which has right left top bottom params, i fill up an array of Pipes and then draw them
I want to draw bottom pipes and top pipes, so 2 arrays joined into the
allPipes
array
i iterate over it and feed the objects to drawRect, to be drawn
if I always use
first
in the loop, I will always get the first of each pair, not both, i want both, so I use two drawRect calls
yep, it works
m

menegatti

08/01/2017, 10:52 AM
out of curiosity, why do you have one list for bottom and another for top pipes?
o

oday

08/01/2017, 10:52 AM
hmm well to make them bottom, their param values have to change
yea i can do a even/odd check inside 1 loop and change the params depending on what I want
lol, yea that’s better