Is there a more idiomatic way to do `for (i in 0 u...
# announcements
d
Is there a more idiomatic way to do
for (i in 0 until someValue)
?
repeat(someValue)
?
o
Depends on the semantics. If
i
is an index into something, then repeat doesn’t convey what’s important. If you just need to do something
someValue
times, then yes,
repeat
is better.
d
I thought repeat passed the index into the lambda. wouldn't the end result be similar?
assuming it was being used to replaced "0 until"
o
While end result might be the same, I think it is important to convey proper meaning, and it depends on the context. E.g. if
someValue
is
someCollection.size
for example, it could be better to iterate on
someCollection.indicies
👍 3
k
Also
repeat
heavily suggests the code in the block does the same every time.
Which is not the case if you're indexing into a collection.