|
From: | Egeyar Bagcioglu |
Subject: | Re: Proposal to change the ranges in array trimming to be "half-open" interval |
Date: | Sun, 1 Nov 2020 13:40:04 +0100 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.3.1 |
On 10/29/20 5:40 PM, Jose E. Marchesi wrote:
Hi Mohammad.Let's talk about ranges in array trimming! ```poke defvar a = [1, 2, 3, 4, 5]; defvar b = a[1:3]; assert (b == [2, 3, 4]); ``` The `1:3` range is an inclusive interval from `1` to `3`. Mathematically, the interval is `[1, 3]`. A "closed interval". IMHO, closed intervals are hard to deal with. Let me explain a little more. This is the scheme used by Poke for indexing elements: 1, 2, 3, 4, 5 ^ ^ ^ ^ ^ | | | | | 0 1 2 3 4 But I argue that the following scheme for indexing elements is much more powerful and easier to deal with: 1, 2, 3, 4, 5 ^ ^ ^ ^ ^ ^ | | | | | | 0 1 2 3 4 5 For a sequence of `N` elements there are `N+1` positions. Positions points **between** elements not **at** elements.Well, in an array subscript a position actually refers to the element following the referred position, right, not the position itself :) Reminds me of the Emacs pointer. But yeah, I like the proposed change; using semi-open intervals makes more sense for zero-based arrays. And now is the time to do changes like this :) Other opinions?
Since you asked... I find it more natural to think about array slices in the following way:The first number is the begin index, while the second number is the number of elements to be found in that array slice.
This logic gives the same results with Mohammad's suggestion for the examples he included below:
```poke' assert (a[0:1] == [1]); assert (a[0:5] == [1, 2, 3, 4, 5]); /* a[0:0] is an empty array */ assert (a[:2] + a[2:] == a); /* instead of `a[:2] + a[2+1:]` */ ``` The difference is when the begin index is non-0.The example array "a" that's been used earlier in this thread is 1-based. Let me use a 0 based array "b" where b = [0, 1, 2, 3, 4].
What I'd prefer is the following: b[2:3] == [2, 3, 4]; What Mohammad suggests: b[2:3] == [2]What I am suggesting here is also how we pass arrays as arguments in C. I find it much more intuitive and I feel very strongly about this approach. Having said that, I'll respect our maintainer's final decision, whatever it will be.
Regards Ege
This is the approached used in `STL` of `C++` (which is designed by Alex Stepanov). The ranges in STL are half-open intervals (mathematically denoted as `[f, l)`). - The number of elements in a range is `l - f`. - Dividing a range into two sub-ranges are simple: `[f, m)` and `[m, l)` New Poke: ```poke' assert (a[0:1] == [1]); assert (a[0:5] == [1, 2, 3, 4, 5]); /* a[0:0] is an empty array */ assert (a[:2] + a[2:] == a); /* instead of `a[:2] + a[2+1:]` */ ``` Regards, Mohammad-Reza Reference: - [Generic Programming](https://youtu.be/iwJpxWHuZQY?t=19m02s) by Sean Parent.
[Prev in Thread] | Current Thread | [Next in Thread] |