Jonathan Ellis
08/09/2022, 3:12 PMval flowed = allShapes.map {
var p = it
for (i in 0 until STRETCH_ITERATIONS) {
p = flow(p)
}
p
}ephemient
08/09/2022, 3:20 PMallShapes.map {
(0 until STRETCH_ITERATIONS).fold(it) { p, _ ->
fold(p)
}
}Jonathan Ellis
08/09/2022, 3:21 PMJoffrey
08/09/2022, 5:27 PMrepeat(n) { ... } function. So in a case where fold wouldn't be a solution to your problem, you could at least replace the loop:
val flowed = allShapes.map {
var p = it
repeat(STRETCH_ITERATIONS) {
p = flow(p)
}
p
}ephemient
08/09/2022, 5:40 PMrepeat, and the rangeTo in a for loop is intrinsified so it doesn't cost anything more than repeatJoffrey
08/09/2022, 5:43 PMbreak and continue, it's not possible. But such cases would probably not be better expressed by repeat anyway - it's not about repeating something N times if you break early.Jonathan Ellis
08/09/2022, 5:44 PMJoffrey
08/09/2022, 5:47 PMrepeat is very clear and meant for this. But of course repeat is for repeating, not for other stuff. That said, I have not needed a for loop in Kotlin for quite some time, there usually are clearer alternatives for most use cases.