Open PavelKoroteev opened 4 years ago
Given that the other sigil is the paren, and parents and brackets both already have a meaning in JS, i don’t think it would make any sense to use mathematical intervals.
Given that the other sigil is the paren, and parents and brackets both already have a meaning in JS, i don’t think it would make any sense to use mathematical intervals.
Fact, that sigil is the paren, and parents and brackets both already have a meaning does not prevent this proposal. Slice method is very useful, but one has some non-obviousness. First arg is included, second is not. Include second make syntax obvious and simplicity, and looks like mathematics intervals.
Ah, you’re suggesting to include the end index, since in math intervals [x,y] would include it. Since math intervals use ,
. and this uses :
, perhaps that’s different enough to avoid the confusion?
Possibly irrelevant but languages in like R and Matlab, the ranges of the form start:end
(e.g: 1:99
) do include the end index the same way [start, end]
does in Maths, so I think it would indeed be more sensical to have the same for this proposal.
Just my 2c!
Ah, you’re suggesting to include the end index, since in math intervals [x,y] would include it. Since math intervals use
,
. and this uses:
, perhaps that’s different enough to avoid the confusion?
Unfortunately, we can't use comma, cause we have comma operator. But we can remove slice non-obviousness without breaking changes.
Right, what I mean is, since comma isn't an option, and math intervals contain commas, I'm not convinced that [x:y]
looks enough like math intervals to create confusion about the y
being exclusive.
There's precedent for both inclusive and exclusive end in various mainstream languages. Several of these languages (like Python) are used to teach beginners so I'm not really convinced that exclusive end is more confusing.
The biggest reason to have an exclusive end is the precedent in Array.prototypes.slice
-- I'd like this to be consistent with the existing slice methods, otherwise it could be confusing when used interchangeably.
How about using this syntax?
[[a:b]]
for range [a, b][(a:b)]
for range (a, b)[(a:b]]
for range (a, b][[a:b)]
for range [a, b)This makes it mathematically correct, and I believe, it doesn't conflict with other syntax.
I'll just add the obvious alternative here, from Ruby and CoffeeScript (and Civet):
[a..b]
for inclusive range [a, b][a...b]
for Pythonic exclusive range [a, b)I didn't quite understand the justification for why not use the CoffeeScript syntax. Perhaps I don't know the grammar well enough, but array[...5]
is different from [...5]
; both can co-exist without ambiguity. But maybe the point is that the visual similarity is confusing, which is a fair point.
In mathematics [] on interval like [1:3] mean that both 1 and 3 include in interval. In current proposal upper index is not included. Maybe we should apply mathematical logic.