dgkf / R

An experimental reimagining of R
https://dgkf.github.io/R
GNU General Public License v3.0
135 stars 6 forks source link

Suggestion for sequence (`:`) operator to require `lhs <= rhs` #162

Closed sebffischer closed 1 month ago

sebffischer commented 1 month ago

One very common bug in R is that people call 1:n in situations where n can be 0. Calling 1:0 leads to the sequence [1, 0], whereas the intended behavior was often to get an empty vector, i.e. integer(0). While there is seq_len in R, it is still something that beginners first have to (un)learn, as calling 1:n seems to be the natural thing to do. Some time ago, I also read an article that this is also a problem in Julia.

I think, we should just make the : operator require lhs <= rhs. For decreasing sequences, we could either refer to a seq() function or implement rev() for reversing a vector (ideally using the future RepType::Iterator.

dgkf commented 1 month ago

Yeah, I'm fine with that. I forgot we had this feature, but you can also define a step using the tertiary : like 1:2:10

If rhs <= lhs, I think it should require the step to be defined (0:-1:-10)