JoeStrout / miniscript

source code of both C# and C++ implementations of the MiniScript scripting language
MIT License
282 stars 64 forks source link

[QoL] range(min, max): max value should not be included #22

Closed andreakarasho closed 5 years ago

andreakarasho commented 5 years ago

Much readable, less space, less chars, more intuitive:

for i in range(0, len(mylist))
    print mylist[i]
end for

Than actual

for i in range(0, len(mylist) - 1)
    print mylist[i]
end for
JoeStrout commented 5 years ago

Even better:

for i in mylist.indexes
    print mylist[i]
end for

Or, of course:

for item in mylist
    print item
end for
JoeStrout commented 5 years ago

So we thought a lot about how range() should behave, and finally settled on the current behavior for a couple of reasons:

  1. It's less surprising to new programmers
  2. It lets you make, say, a counter from 1 to 10 by doing range(1,10) rather than range(1,10.1) or range(1,11)
  3. It's the only behavior that makes sense in general with floating-point numbers, e.g. range(0.5, 0.8, 0.1), and in MiniScript all numbers are floating-point
  4. As noted above, when you're trying to iterate over a collection, there are better ways of doing that anyway.

I do understand this behavior may be surprising to people that have done a lot of coding in other languages, though, where the semi-open interval is standard and convenient for integer ranges.

andreakarasho commented 5 years ago

Make sense thanks