poke-devel
[Top][All Lists]

## Re: Proposal to change the ranges in array trimming to be "half-open" in

 From: Dan Čermák Subject: Re: Proposal to change the ranges in array trimming to be "half-open" interval Date: Sun, 01 Nov 2020 21:20:51 +0100

```Hi Ege,

Egeyar Bagcioglu <egeyar@gmail.com> writes:

> On 10/29/20 5:40 PM, Jose E. Marchesi wrote:
>>
>>> 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?
>
>
> 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];
>
> b[2:3] == [2]
>
> What I am suggesting here is also how we pass arrays as arguments in C.
> approach. Having said that, I'll respect our maintainer's final
> decision, whatever it will be.

I like this idea as well but I'm opposed to going this way solely for
the reason that afaik all other languages that have builtin array slices
implement them the way Mohammad suggested. Besides that I honestly
don't really care, but I think for the sake consistency and onboarding
experience, I'd go with the "standard" way.

Cheers,

Dan
```

signature.asc
Description: PGP signature