tc39 / proposal-slice-notation

http://tc39.es/proposal-slice-notation/
MIT License
523 stars 18 forks source link

Mathematics intervals #41

Open PavelKoroteev opened 4 years ago

PavelKoroteev commented 4 years ago

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.

ljharb commented 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.

PavelKoroteev commented 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.

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.

ljharb commented 4 years ago

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?

Berkmann18 commented 4 years ago

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!

PavelKoroteev commented 4 years ago

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.

ljharb commented 4 years ago

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.

gsathya commented 4 years ago

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.

pavi2410 commented 4 years ago

How about using this syntax?

This makes it mathematically correct, and I believe, it doesn't conflict with other syntax.

edemaine commented 10 months ago

I'll just add the obvious alternative here, from Ruby and CoffeeScript (and Civet):

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.