3 ms·
Why does that matter when the starting point is 1? In that case, you don't need the delta because you have the length already. In the case of a 0-based range yo
by Jtsummers 4y ago
Why does that matter when the starting point is 1? In that case, you don't need the delta because you have the length already. In the case of a 0-based range you also don't need the delta, though you will want to use the inclusive-exclusive convention so that you get the length "for free".
1 <= a <= N -- N = length, no delta
0 <= a < N -- N = length, no delta
You only calculate the length when dealing with other than 0- or 1-based ranges. There, the inclusive-exclusive convention is very handy as you point out. So if we're fixing the initial offset at either 0 or 1, then use the appropriate convention for that offset. If we let the initial offset float, then the inclusive-exclusive makes sense.
- layer8 4y agoIt matters whenever you need to process a proper subrange of an array, and that shouldn’t be different from when the subrange happens to be the whole range. It’s simpler if the same convention is used in all cases. E.g. in Java, a typical example is that OutputStream has the following two methods, where the first can delegate to the second, and the second (which write the specified subrange of the array) can easily calculate the number of bytes to write: int write(byte[] bytes) { return write(bytes, 0, bytes.length); } int write(byte[] bytes, int start, int end) { int length = end - start; ... }
- Jtsummers 4y agoI addressed that: > If we let the initial offset float, then the inclusive-exclusive makes sense. But you replied to someone using inclusive-inclusive for 1-based arrays and complained about the delta not matching the length. Which is a nonsensical complaint, you have the length already why would you need to calculate anything?
- layer8 4y agoIn case of the subrange you don’t have the length (or you conversely have the length but not the upper bound), and you don’t want to use different conventions based on whether you process the subrange or the whole range. I’m not sure how I can make it clearer.