-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider first-class type (i.e. 'NumberRange' or 'Interval') vs. a 'range' method #22
Comments
I'm trying to avoid adding a global name on the On the other hand, the slice notation can behave the following way // Literal `Range` syntax:
let range = 1:3;
// -> range = Number.range(1, 3); And I will add |
I'm opposed to a syntax that would require a |
There's no reason |
Value type for range? Do you mean the record and tuple or a separate new value type? (I don't think it should be a value type) |
If so, I would prefer
But not need to limit to We can also make the class work for non-numeric types, and rename it as |
|
Value types are pretty far out. If we had them now, it could make sense. I think this proposal fills an urgent need, and we shouldn't wait on value types for them. I think this proposal will be very very useful with range iterables (if we have them at all) being objects. Most of the time, you won't really use the iterable much at all; you'll just get its iterator. The Temporal proposal is making a similar tradeoff. |
What would happen on property access with this new built in type? const r = Range(1, 3);
[1, 2, 3, 4, 5][r]
// [2, 3] or undefined? |
(Note your code have ASI problem) I don't think we should overwrite the behavior of [ ]. (I think this belong to the slice notation proposal) |
Why? |
Maybe things like [r.from:r.to] or other notation. I'm not preferring to overload the [expr] |
I agree. I'm not sure we'd want different behavior for slice notation depending on where it's used. Not a fan of this: const r = 1: 3; // creates a Range object
[1, 2, 3, 4, 5][r]; // undefined
[1, 2, 3, 4, 5][1: 3]; // [2, 3] I'd prefer this: const f = 1:3; // SyntaxError
const r = Range(1, 3);
[1, 2, 3, 4, 5][r]; // undefined
[1, 2, 4, 5, 5][r.from: r.to]; // [2, 3]
[1, 2, 3, 4, 5][1: 3]; // [2, 3] |
Perhaps as part of a different proposal, it would be good to introduce some sort of getter/setter protocol. If |
late to this issue, but wanted to re-up the notion of a EDIT: striking discussion on reusability, as I think it's actually just orthogonal to this issue. regardless, curious to hear thoughts on |
I'm not sure what you mean by ".includes" - you mean a method on the iterator itself? |
What would be the semantics of an proposal-iterator-helpers has a spec for proto.includes = function (searchValue) {
return this.some(value => SameValueZero(value, searchValue));
}; If the semantics are likely to be different than those that might be specified elsewhere, it may be preferable to consider using a different name for the suggested method. |
yep, like
I think yes, it would have to consume the iterator.
I could see this going either way. should
I think so, yes. I actually wasn't aware of the iterator helpers proposal - |
I think it would be slightly more useful to instead have something like Math.inInterval = function (number, inclusive, exclusive) {
if (inclusive < exclusive) {
return inclusive <= number && number < exclusive;
}
return exclusive < number && number <= inclusive;
}; This, or something similar, could be specified in https://github.com/rwaldron/proposal-math-extensions We would then have var range = Number.range(0, 10);
Math.inInterval(5.5, range.start, range.end); // true Note (2022-07-14): there is a proposal for a similar function at https://github.com/js-choi/proposal-math-between |
Yes, I agree, but the |
Consider slice notation seems have trouble to advance, I think we could reconsider the ideas of |
Sometimes it is very handy to get range of array, but overloading [] is too much, maybe it would be nice for Interval/range to be callable, like: const rows = [ ['a','b','c','d'], ['d','e','f','g'], ['q','w','e','r']];
(1..2)(rows) == [['d','e','f','g'], ['q','w','e','r']];
rows.map(1..2) == [ ['b','c'], ['e','f'], ['w','e']]; |
I think a range should be in Number, because we are talking about use a range of numbers, create a new class of Interval could be too much just to a range. |
looks like a first-class value is very far from us... closing this for now but still welcome discussions |
In tc39/proposal-slice-notation#19 (comment) I suggested adding something similar to the C#
Index
andRange
types which could also address these use cases with a more flexible API than an overloadedNumber.range
method.An example of these APIs can be found here:
Index
- https://github.com/esfx/esfx/blob/c50876e542bc73df907b5e3ff4ef95ad656cb3b3/packages/interval/src/index.ts#L23Interval
- https://github.com/esfx/esfx/blob/c50876e542bc73df907b5e3ff4ef95ad656cb3b3/packages/interval/src/index.ts#L107The advantage of this (or a similar) API is that it provides a convenient place to attach functionality like
.includes(value)
, as well as a place to provide different ways to generate anInterval
without relying on possibly confusing overloaded behavior likeNumber.range(10)
.The text was updated successfully, but these errors were encountered: