I often find a situation where I need to write duplicate codes in a for loop, where the "init step" to identical to the "update step":
// duplicate `next()`
for (let x = next(); p(x); x = next()) {
f(x)
}
// the same in a while loop
let x = next()
while (p(x)) {
f(x)
x = next()
}
It is not much a problem when next()
is short, but often it's not:
// problem is evident when next is long or even inline
let x = mmmmm(rewqreqw(rewqrqew()), rewqrewq(), rewqreqw())
while (p(x)) {
f(x)
x = mmmmm(rewqreqw(rewqrqew()), rewqrewq(), rewqreqw())
}
How can I deal with this situation?