[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [RFC] Syntax for appending elements to arrays
From: |
Jose E. Marchesi |
Subject: |
Re: [RFC] Syntax for appending elements to arrays |
Date: |
Fri, 27 Nov 2020 08:33:56 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
> On Thu, Nov 26, 2020 at 09:34:52AM +0100, Jose E. Marchesi wrote:
>>
>> I just realized 2) is unfortunately ambiguous with the existing
>> array-concatenation operation. If you have:
>>
>> var a = [[1,2],[3,4]];
>>
>> And you do:
>>
>> a += [5,6];
>>
>> Are you actually trying to concatenate the arrays [[1,2],[3,4]] and
>> [5,6], in which case you want a syntax error, or
>>
>> Are you actually tring to append an element [5,6]?
>>
>> If no one comes with some sane syntax for this, we will have to resort
>> to the usual notation:
>>
>> a.push ([5,6]) or,
>> a.append ([5,6])
>>
>> But I would really want to avoid that if possible..
>>
>> No more ideas, anyone?
>>
>
>
> Despite the fact I may miss something obvious, I try to explain my approach
> to deal with`lhs += rhs` in pseudo-code:
>
> if (type (lhs) == ARRAY)
> {
> if (arr_elem_type (lhs) == type (rhs))
> arr_append_elem (lhs, rhs);
> else if (type (rhs) == ARRAY &&
> arr_elem_type (lhs) == arr_elem_type (rhs))
> arr_concat (lhs, rhs);
> else
> error ();
> }
> /* ... */
Yes, I see that logic. But I think it is way too complicated and can
easily lead to confusion.
> So, here we will have:
>
> ```poke
> var a = [[1,2],[3,4]];
>
> a += [5,6];
> assert (a == [[1,2],[3,4],[5,6]]);
> ```
> This approach is also backward-compatible with current Poke codes.
>
> But if this is too complicated to implement or understand, introducing an
> `append` function is not that bad.
> Also having an `extend` function is desirable.
>
> ```poke
> a = [1,2,3];
> b = [4,5];
>
> a.extend(b);
> assert (a == [1,2,3,4,5]);
> ```
If we go this way, I would take inspiration from Icon (once again) and
have operations:
([1,2,3]).push (4)
- Side-effect: [1,2,3] -> [4,1,2,3]
- Evals to [4,2,3,4]
([1,2,3]).pop
- Side-effect: [1,2,3] -> [2,3]
- Evals to 1.
([1,2,3]).put (4)
- Side-effect: [1,2,3] -> [1,2,3,4]
- Evals to [1,2,3,4]
([1,2,3]).pull
- Side-effect: [1,2,3] -> [1,2]
- Evals to 3
- [RFC] Syntax for appending elements to arrays, Jose E. Marchesi, 2020/11/25
- Re: [RFC] Syntax for appending elements to arrays, Mohammad-Reza Nabipoor, 2020/11/25
- Re: [RFC] Syntax for appending elements to arrays, Dan Čermák, 2020/11/25
- Re: [RFC] Syntax for appending elements to arrays, Jose E. Marchesi, 2020/11/26
- Re: [RFC] Syntax for appending elements to arrays, Mohammad-Reza Nabipoor, 2020/11/26
- Re: [RFC] Syntax for appending elements to arrays,
Jose E. Marchesi <=
- Re: [RFC] Syntax for appending elements to arrays, Jose E. Marchesi, 2020/11/27
- Re: [RFC] Syntax for appending elements to arrays, Egeyar Bagcioglu, 2020/11/27
- Re: [RFC] Syntax for appending elements to arrays, Jose E. Marchesi, 2020/11/27
- Re: [RFC] Syntax for appending elements to arrays, Egeyar Bagcioglu, 2020/11/27
- Re: [RFC] Syntax for appending elements to arrays, Jose E. Marchesi, 2020/11/27