is there a cool new flashy kotlin way to iterate o...
# getting-started
o
is there a cool new flashy kotlin way to iterate over 2 lists of the same size in Kotlin?
k
zip
?
o
zip…ok I’ll look into it, makes them into 1 list that you can iterate over?
m
map elements at the same index to a pair and then you can iterate as usual
o
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
a pair has
first
and
second
attributes
o
yes
k
Or you could destructure. Something like:
Copy code
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
I add “Pipe” object which has these params, and i use those pipes to draw them with a drawRect
Copy code
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
I don't follow
o
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
out of curiosity, why do you have one list for bottom and another for top pipes?
o
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