-
Notifications
You must be signed in to change notification settings - Fork 1
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
Ranges with strides #174
Comments
Languages like Haskell have already dealt with this issue. Haskell arithmetic sequences can be written as [1 .. 30] or [1,4 .. 30]. The first is similar to what we now support, while the second gives the implicit gap (4-1 or 3). We could do the same thing, but without the square brackets. I don't like your proposed notation as the two sets of ".." mean different things -- the first for a range and the second for the gap. I suspect it is illegal with our naming rules, but 1 .. 30 by 3 would be more attractive to me. Alternatively we could have .. give the range and have a "by" method that filters the range. I.e., 1..30.by 3 would indicate the range where you grab the 1st, 4th, etc. elements. At any rate, I'm not a big fan of your proposal -- and I wouldn't even mind writing "rangeFrom 1 to 30 by 3" when you need the stride different from 1. |
I presume that you are not opposed to the idea of creating strided range objects, and that what we are discussing here is syntax. I am also aware that other languages have addressed this problem; Fortress, for example, uses We could indeed do what you are proposing and invent some new syntax. Haskell's range notation is an example of that: In this proposal, I am trying to avoid changing the language and do this in a library with Grace operators, which is what we have done so far with I'm proposing that we look at syntaxes that are already legal before we resort to extending the language. Of your suggestions, A different approach would be to use another operator for downward ranges; something like |
Yet another possibility is to define
and so on. This may be the simplest notationally, but encumbers the |
I'd go with |
I don't understand the "fun question". I've implemented |
supposed to be |
Initially, I thought that |
well I kind of think it makes sense. Not clear how Consider |
Non-integer strides clearly make no sense. Neither do negative strides, with this interpretation — what would a stride of -2, meaning "every negative other element" mean? However, if we assume the meaning of |
While I agree with Andrew on positive integer strides, I continue to hate the notation! |
If One option is to make a negative stride mean "first reverse the target, and then apply the negation of the stride". (I think that this is equivalent to: start striding from the upper bound, and add the (negative) stride to the index at each step.) So,
I also don't love the double use of
As a side effect of thinking about this, I realize that
|
I don't see the problem of a range vs a collection - I mean, isn't a range |
Neither do I. Ranges are just an implementation of Sequence. |
I've long found it annoying that, while we have a nice notation for ascending ranges,
we don't have one for descending ranges like
[4, 3, 2, 1]
. No, we can't allow4..1
, because that's an empty range.(1..4).reversed
works, but is long-winded.range.from 4 downTo 1
is the primitive constructor, and it's even longer-winded.A possible soution is to devise a general notation for "strided ranges" (or should it be "stridden" ranges?) For example
This could be implemented pretty easily by defining the
..
operator on ranges.The text was updated successfully, but these errors were encountered: