Possible Duplicate:
Decreasing for loop in Scala?
While working through Scala For The Impatient, I came upon the following exercise:
Write a Scala equivalent for the Java loop for (int i = 10; i >= 0; i--) System.out.println(i);
It did not take me long to come up with the following solution:
for (i <- 1 to 10 reverse) {
println(i)
}
However, this made me wonder about how to reason about the cost of doing this. Does the reverse method do an O(n) traversal of the Range, or does it decorate it with something that does fancy index arithmetic? Are there other constructs that can do this better?