extending array macro language: array[] = expr

Tony Balinski ajbj at free.fr
Thu Apr 10 22:56:30 CEST 2008


Quoting Bert Wesarg <bert.wesarg at googlemail.com>:

> On Thu, Apr 10, 2008 at 10:07 AM, Joachim Lous <joachim at lous.org> wrote:
> > On Thu, Apr 10, 2008 at 6:23 AM, Tony Balinski <ajbj at free.fr> wrote:
> >  >  A bit misleading this: if you have the array
> >  >   arr[0] = 0
> >  >   arr[1] = 1
> >  >   arr["key0"] = 2
> >  >   arr["key1"] = 3
> >  >  adding arr[] = 999 will provide arr[2], not arr[4] as I would expect
> from the
> >  >  right-hand-side behaviour of arr[] (it's not the same as arr[arr[]] =
> 999). I
> >  >  find this counter-intuitive.
> >
> >  Maybe so, but the only other common languages I know that mix numeric
> >  and associative arrays in the same object are Javascript and PHP, and
> >  both assign numeric keys in this way when you push an unnamed value
> >  into an array (although with Javascript you have to call "push", not
> >  just use an empty bracket)
> >
> I didn't know about JS, but I have modeled this after the PHP behaviour.
>
> But I can understand the point from Tony and Aaron.
>
> Maybe a drastically syntax extension is needed:
>
> rvalue: arr[@]        := return max numeric index + 1
> lvalue: arr[@] = expr := push expr at position max numeric index + 1

A "push()" function would work. I don't much like in the above the
notion of lhs arr[@] accessing an element while rhs arr[@] gives an index.
With the above rules, the following statements are equivalent:
          arr[@] = expr
          arr[arr[@]] = expr

I see the following needs:
1. "reseating" indices (starting at 0 to starting at 1, say);
2. removing holes in a sequence;
3. concatenating sequences;
4. finding first and last index values.

How about shift operators? (These don't exist for integers yet BTW)
They would shift indices as follows:
eg
  arr >>= 1     # reorganise num indices to start with 1 in array arr
  arr <<= 10    # reorganise num indices to end with 10-1 in array arr

the result contains a sequence in which all numeric indices have been
renumbered with consecutive indices (perhaps negative) to satisfy the
contraint of a fixed start/end position. So, given the args() function
("define args { return $args }") and my anonymous array constructor
syntax,

  args(1, 2, 3, .hello="hi")  equals  { 1, 2, 3, .hello = "hi" } >> 1

When both arguments to shift are arrays:

  arr2 >> arr1  # an array where sequence arr2 has indices after arr1
  arr1 << arr2  # the opposite
  arr1 <<= arr2 # add arr2 sequence to end of arr1
  arr1 <<= { value } # add a value to the end of the sequence in arr1

For each of these, numeric indices are compacted to start at the lowest
valued index from arr1 and grow consecutively to address all numerically
accessed values up to the last from arr2, while doing the equivalent of
arr1 + arr2 for string-accessed values (ie the union of string indices,
with, for duplicate indices, values taken from arr2).

Or maybe just allow array concatenation to do that:
  arr1 arr2

Hmmm.

Tony


More information about the Develop mailing list