4 ms·
You could start indices at 0 but still take slices with [start:end], I don't see a dependency between those two aspects. And the start index being 0 or 1 does n
by henearkr 3y ago
You could start indices at 0 but still take slices with [start:end], I don't see a dependency between those two aspects. And the start index being 0 or 1 does not affect much the understanding of the language, contrary to this slice insanity in Python...
I had college lectures using 0 as start index, others using 1, both were fine, no problem.
However taking a slice with [start:end+1] is unequivocally counterintuitive.
- mejutoco 3y agoThey are orthogonal issues. The connection was that if Python can be counterintuitive because of slice syntax, the same can be claimed of Julia and 1-indexed. It is not a good argument IMO. I see your point, but personally find it a matter of preference, not of objective truth. `range(end+1)` is consistent with that slice notation that you disapprove of. At the same time, on 0-indexed vs 1-indexed you seem to have no preference (I prefer 0 for historical reasons). How about `len(items)` vs `items.length`? I have my own preferences there, but I would not say any or the other is intrinsically better. Anyway, I already wrote way too much about such a small syntax thing :)
- henearkr 3y agoBut why should the slice notation be consistent with the range() syntax, which is itself weird? They could have added, in parallel to range(start,end+1), some interval() function that would adopt the more sensible syntax interval(start,end), and then design the slice syntax on this new interval() one. And to begin with, why on Earth is range() using the syntax range(start,end+1)? I honestly can't understand why it was so important for the language designers to calque it on the (barely) most common idiom in C "for (int i=begin; i<end+1; ++i)" which can by the way very well be replaced by "for (int i=begin; i<=end; ++i)". While they were at designing a sugar syntax for slicing (and the same applies for range), why didn't they internalize the "i<end+1" quirk by doing the conversion in the implementation code??? > How about `len(items)` vs `items.length`? This, poses no problem to me. Just a different language, nothing hurting my sense of logic like [start:end+1] does. Same for 0 vs 1, just convention, fine by me.
- 331c8c71 3y agoPython's slice/range semantics never occured to be really weird to me because half-open intervals [x,y) are widely used in measure theory textbooks.
- henearkr 3y agoHow hard can you continue to advocate for this way of asking like: "please count from 0 to 7 excluded"? Ask this to anybody in the street and they'll be confuse why you mentionned 7 if it's excluded! That's natural numbers, not reals! > measure theory First of all, even for a specialist, that extremely hard to be delusioned into thinking we're doing measure theory, when we're manipulating indices of arrays/matrices/tensors. But anyways, assuming you think it's measure theory. I'm trained in the field and that's impossible to expect half-open intervals in this particular place! To the contrary: the support of base elements of major functional spaces used in measure theory textbooks are always compacts.
- 331c8c71 3y ago> How hard can you continue to advocate ... That's natural numbers, not reals! I didn't mean to advocate at all... Just explained that I never peronally felt the weirdness of it. The point about integers is taken but Python style also gives you len(range(n, n+k)) == k and len(range(n, k)) == n - k which is very convenient at times. > To the contrary: the support of base elements of major functional spaces used in measure theory textbooks are always compact. You can take either type of intervals to define Borel sigma algebras. Half-open intervals, however, additionaly form a semiring while finite unions of half-open intervals additionaly form a ring - the starting point of Caratheodory's extension theorem [1] which is kind of essential at least in some expositions. [1]: https://en.m.wikipedia.org/wiki/Carath%C3%A9odory%27s_extension_theorem https://en.m.wikipedia.org/wiki/Carath%C3%A9odory%27s_extens...