poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Jose E. Marchesi
Subject: Re: Proposal to change the ranges in array trimming to be "half-open" interval
Date: Mon, 02 Nov 2020 21:03:07 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

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

Not all.  Algol68 uses inclusive ranges ;)

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

I agree on adoptin the "standard" way.  Poke is weird enough as it
is...

That said, Egeyar's suggestion is definitely intriguing.  It is was to
be supported at some point, however, I would use a very different
syntax.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]