Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Enhancing array indices

5 views
Skip to first unread message

Jonathan Lang

unread,
Jan 30, 2007, 5:54:26 PM1/30/07
to p6l
Could we get a single-character symbol that could be used in an array
index to refer to its shape in a dwimmy way? Something like:

@x[*.head]
@x[*.tail] # equivalent to @x[*]
@A[*.head+2, *.tail-1]

(where head and tail are methods of the shape that return the current
dimension's start and end points.)

--
Jonathan "Dataweaver" Lang

Larry Wall

unread,
Jan 30, 2007, 6:47:34 PM1/30/07
to p6l
On Tue, Jan 30, 2007 at 02:54:26PM -0800, Jonathan Lang wrote:
: Could we get a single-character symbol that could be used in an array

This mornings up date proposed

@x[*+0]
@x[*-1]
@A[*+2, *-1]

for that. But maybe you meant @x[*+2 .. *-1]? And @x[*] would be

@x[*+0..^*-0]

written out that way. Or possibly

@x[-* ..^ +*]

depending on how we define the unaries.

[BTW, I'm suddenly wondering now if spam filters are weeding out
the synopsis updates for certain people...I've noticed that when I
respond to a thread by doing a synopsis update it tends not to end
up in the summaries, for instance. Maybe gmail thinks they're spam
because of too much self-similarity?]

Larry

Larry Wall

unread,
Jan 30, 2007, 6:51:27 PM1/30/07
to p6l
On Tue, Jan 30, 2007 at 03:47:34PM -0800, Larry Wall wrote:
: This mornings up date proposed

Now the da rn spam fi1ters are chang.ng my spelling to look like sp*m.
Yeah, that's the 4icket... :)

Larry

Darren Duncan

unread,
Jan 30, 2007, 7:18:27 PM1/30/07
to p6l
At 12:11 PM -0800 1/30/07, la...@cvs.develooper.com wrote:
>Log:
>Disabled negative subscript dwimmery for all shaped arrays.
>* can now take + and - operators.

In a related fashion ...

I'm thinking it may be better to just get rid of negative subscript
dwimmery entirely, eg "@foo[-1]", and use things like "@foo[*.head]"
and "@foo[*.tail]" instead to refer to the first and last elements.

That solution is a lot more self-describing, leads to better
consistency between shaped and unshaped arrays, and is easy to learn
for both Perl 5 people and people coming from other languages.

Also importantly, it should prevent common types of errors.

If being more terse is important, you could further abbreviate .head
and .tail, but the main point is that a plain -N array index never
means count from the end.

-- Darren Duncan

Jonathan Lang

unread,
Jan 30, 2007, 8:42:22 PM1/30/07
to p6l
Larry Wall wrote:
> And @x[*] would be
>
> @x[*+0..^*-0]
>
> written out that way. Or possibly
>
> @x[-* ..^ +*]
>
> depending on how we define the unaries.

Hmm... how about this:

Normally, * in the context of an indexer acts as a Range object,
covering the range of available indices (defined inclusively). I'm
assuming that Range objects provide methods for accessing the lower
and upper bounds of the range (probably 'min' and 'max'); these
methods could be used to refer to the start and end of the array
explicitly.

When coerced to Num, * points to the first element of the array and
enables negative-index dwimmery relative to it - i.e., '* + Int'
(where Int < 0) counts from the end (with '*-1' being the end), while
'* + Int' (where Int >= 0) counts from the start (with '*+0' being the
start). Or perhaps it uses Oroborus dwimmery, with too-large indices
wrapping back around to where they started: @x[*+5] is the same as
@x[*+0] for a five-element array.

So:

@x[*] # every element

@x[*.min] # first element
@x[*.min+0] # first element
@x[*.min+1] # second element
@x[*.min-1] # just before first element

@x[*.max] #last element
@x[*.max+0] # last element
@x[*.max+1] # just after last element
@x[*.max-1] # second to last element

@x[+*] # first element
@x[*+0] # first element
@x[*+1] # second element
@x[*-1] # last element

Unshaped arrays work exactly like shaped arrays; so perl 5's '$x[-1]'
becomes '@x[*-1]', while '@x[-1]' is a reference to just before the
first element (and assigning a value to it shapes the array to include
it within the lower bound).

That said, I'd like to be able to spell '[*.min]' as '[1st]',
'[*.min+1]' as '[2nd]', and '[*.max]' as '[last]'; but I could live
without 'em.

--
Jonathan "Dataweaver" Lang

TSa

unread,
Jan 31, 2007, 9:40:12 AM1/31/07
to p6l
HaloO

Larry Wall wrote:
> But maybe you meant @x[*+2 .. *-1]? And @x[*] would be
>
> @x[*+0..^*-0]
>
> written out that way.

How far can we drive the dwimmy use of the three values
that determine array access, i.e first and last index and
length. E.g. would @x[*+0..*/2] roughly slice to the middle
of the array? The second * needs to use the last index to
care for the possibility of an array with a negative shape.
Well or it uses the first index after the array. This would
also fit the @x[*+0..^*-0] above.

Hmm, slicing the second half @x[*+*/2..*-1] is even dwimmier.
It needs the length of the array for the second * to jump in
positive direction.

Regards, TSa.
--

TSa

unread,
Jan 31, 2007, 12:18:56 PM1/31/07
to p6l
HaloO,

I wrote:
> E.g. would @x[*+0..*/2] roughly slice to the middle
> of the array?

Hmm, this might not work. It should be @x[*+0..*-1-*/2]
on the footing that the last * is dwiming the length.
This implies that dispatches to &infix:<->:(Whatever,
Whatever) and &infix</>:(Whatever,Int) are supported
and create the respective Whatever values. The range
Whatever value in the end has got three parameters:
the first array index, the first index after the array
and the array length. This might be a bit too complicated
to be really useful.

Regards, TSa.
--

0 new messages