3 ms·
I think this is mostly to keep in line with indexing, where you have that same sort of semi-inclusion. 'example string'[3:8] # gives you 5 characters.
by camel_Snake 9y ago
I think this is mostly to keep in line with indexing, where you have that same sort of semi-inclusion.
'example string'[3:8] # gives you 5 characters.
- nextlevelwizard 9y agoBut wouldn't you rather just say: "hey, starting from the 3rd index, give me 5 letters" like 2.3.3 :001 > 'example string'[3, 5] => "mple " Instead of having to think before hand what indexes thous would be.
- int_19h 9y agoIt depends on what you're trying to do. It's pretty common with strings to ask for "everything excluding the first and the last character", for example, and then you want the end index (counted backwards), not length. Another common case is when you looked up a substring and got an index, and now you're slicing the string around that index. But there are also many cases where length is more desirable. Ditto with ranges - both end-inclusive and end-exclusive ranges are useful in different cases. I kinda like the fact that Ruby lets you choose, but I think that .. vs ... syntax distinction is too subtle and likely to cause bugs. I kinda like the way Nim does it: .. for inclusive, and ..< for exclusive - e.g. 1..<10. They also use special syntax for count-from-end, instead of negative indices, which is also a good thing IMO (the decision to index from start or from end is usually something that's not going to change at runtime; but with negative indexing, it might inadvertently do so if you underflow when computing the index).