nim-lang / RFCs

A repository for your Nim proposals.
135 stars 26 forks source link

For loop in reverse order #425

Closed al6x closed 2 years ago

al6x commented 2 years ago

Sometimes it's useful to iterate in reverse order

for i in 10..1:
  echo i
konsumlamm commented 2 years ago

It would be very helpful if this RFC included more information, like more motivation. alternatives, drawbacks, etc. See the other RFCs for examples of that.

This has been requested quite often and it has been explained equally often that "reverse ranges" (a..b where a > b) are empty for a reason, namely that things like 0..(a.len - 1) would silently be buggy. As another data point, Rust also has this behaviour. Apart from that, it would be a (silent) breaking change, which is yet another reason against changing this.

Additionally, this can already be achieved by using the countdown iterator:

for i in countdown(10, 1):
  echo i
Varriount commented 2 years ago

While this makes sense logically, practically there are a few points against it:

al6x commented 2 years ago

Ok, closing then.

Additionally, this can already be achieved by using the countdown iterator:

There are so called Machete vs SwissKnife designs. With machete, you have simple knife that could do many things, and the more you master it the more you can do. With SwissKnife, you have a box of predefined narrow use tools and you can pick tool you need, but you can't quite combine it or master the same way.

I think this is about this situation, where you can't reuse a "Range" tool in new situation, and need to pick up another specific "countdown" tool.