Reputation:
Consider the following loop that represents a "neighborhood"
for(x <- -1 to 1; y <- -1 to 1)
{
// If we are in the current field, just get the one above
if((x == 0) && (y == 0)) y = 1 // Problem: Reassignment to val
}
as you can see, I'd get a reassignment to val compilation error. In Java I would do "continue" to skip that.
What would be an elegant solution to this?
Upvotes: 2
Views: 1128
Reputation: 29772
Use a guard:
for (x <- -1 to 1; y <- -1 to 1; if !(x == 0 && y == 0)) {
...
}
Alternately:
for (x <- -1 to 1; y <- -1 to 1 by (if (x == 0) 2 else 1)) {
...
}
I think the first one is more readable, though.
Upvotes: 5