Re: RFC 204 (v2) Arrays: Use list reference for multidimensional array access

2000-09-27 Thread Nathan Wiger

Bart Lateur wrote:
 
 The problem is that
 
$name = "myarray";
@$name = (1,2,3);
print @$name[0,1];  # 1,2
 
 Is very consistent currently. Change one and you have to change the
 precedence and parsing of all symbolic refs.
 
 You are suggesting to keep a weird precedence rule, just to ease
 symbolic dereferencing!?! That's... obscene.

Where did I say that?! I even said:

   I'm not necessarily against it, but it's a deep issue not just
   constrained to arrays in this one specific context.

My point was if this idea is RFC'ed, the RFC better not be titled "Fix
arrays" but better be called "Redo the precedence rules of all symbolic
references".

-Nate



Re: RFC 204 (v2) Arrays: Use list reference for multidimensional array access

2000-09-26 Thread Bart Lateur

On Mon, 25 Sep 2000 19:26:38 -0700, Nathan Wiger wrote:

 I agree with both of you. It would be nice if @$ precedence worked as Bart
 specified, but I still think that arrays should be arrays.

The problem is that

   $name = "myarray";
   @$name = (1,2,3);
   print @$name[0,1];  # 1,2

Is very consistent currently. Change one and you have to change the
precedence and parsing of all symbolic refs.

You are suggesting to keep a weird precedence rule, just to ease
symbolic dereferencing!?! That's... obscene.

-- 
Bart.



Re: RFC 204 (v2) Arrays: Use list reference for multidimensional array access

2000-09-21 Thread Bart Lateur

On 20 Sep 2000 04:06:02 -, Perl6 RFC Librarian wrote:

Ilya Zakharevich brought up the issue of a potential problem with
objects which use blessed list references as their internal structure,
and their use as indices.  Given a Bignum class, which stores its
(external) value internally as a list of integers, doing something
like:

my $bignum = Bignum-new(23) # stored internally as [ 2, 3 ]
print $array[$bignum]; # should it print $array[23] or $array[[2,3]]?

can be ambiguous.  I'm not so sure.  I think that $bignum is not
legal, under Perl5, to be an array index, and that if it is going to
be use so, it would need to be $array[$bugnum-value] anyway.

Hmm... the problem is, I think, that array references and ordinary
scalars are both scalars.

What would be the difference between

$a[2]

and 

$a[[2]]

anyway? Aren't these just the same?

If so, why not grab back into the old box, and get the syntax for
"multidimensional hashes" in perl4?

single dimension:   $hash{$item}

2 dimensions:   $hash{$item1, $item2}

Note that because of the '$' prefix, this cannot be confused with a
narray slice:

hash slice, not multidimensionanl hash: @hash {$item1, îtem2}

So, the similar syntax for ordinary arrays would then be:

$array[2, 3]

not $array[[2, 2]]

Please feel free to corrct me if I'm wrong.

-- 
Bart.



Re: RFC 204 (v2) Arrays: Use list reference for multidimensional array access

2000-09-21 Thread Jeremy Howard

Bart Lateur wrote:
 Hmm... the problem is, I think, that array references and ordinary
 scalars are both scalars.

That's true, but they're scalars with different interfaces. In particular,
an array ref can be dereferenced and provides an array in doing so. If an
index can do this, then it's a multidimensional index. Of course, this isn't
how it would actually be handled internally, since it's inefficient, but
this is how its language interface would look.

 What would be the difference between

 $a[2]

 and

 $a[[2]]

 anyway? Aren't these just the same?

Nearly! From the RFC:

quote
When a listref is used to index a list of lists, the returned list reference
is automatically dereferenced:

  my @array( [0,1], [1,2]);
  my @a = @array[[0]]; # Returns (0,1), _not_ [0,1]
/quote

Indexing with an integer doesn't have this feature. I considered proposing
adding it (so that all list context assignments dereference the rvalue if
it's a list ref), but I think it would lead to too many incompatibilities
with P5.

 If so, why not grab back into the old box, and get the syntax for
 "multidimensional hashes" in perl4?

 single dimension: $hash{$item}

 2 dimensions: $hash{$item1, $item2}

 Note that because of the '$' prefix, this cannot be confused with a
 narray slice:

 hash slice, not multidimensionanl hash: @hash {$item1, îtem2}

 So, the similar syntax for ordinary arrays would then be:

 $array[2, 3]

 not $array[[2, 2]]

 Please feel free to corrct me if I'm wrong.

The difficulty with this is that it's not clear how to specify a
multidimensional slice. An index is either multidimensional, *or* a slice,
but not *both*.





RFC 204 (v2) Arrays: Use list reference for multidimensional array access

2000-09-19 Thread Perl6 RFC Librarian

This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

Arrays: Use list reference for multidimensional array access

=head1 VERSION

   Maintainer: Buddha Buck [EMAIL PROTECTED]
   Date: 8 Sep 2000
   Last Modified: 19 Sep 2000
   Mailing List: [EMAIL PROTECTED]
   Number: 204
   Version: 2
   Status: Frozen

=head1 ABSTRACT

This RFC proposes that array indexing syntax be extended to use references
to integer lists as indices for multidimensional arrays.

=head1 SUMMARY OF DISCUSSION

Most of the discussion concerning this RFC accepted it as-is as a workable
syntax.  It meets most of the requirements for multidimensional array access
that have been discussed.

Ilya Zakharevich brought up the issue of a potential problem with
objects which use blessed list references as their internal structure,
and their use as indices.  Given a Bignum class, which stores its
(external) value internally as a list of integers, doing something
like:

my $bignum = Bignum-new(23) # stored internally as [ 2, 3 ]
print $array[$bignum]; # should it print $array[23] or $array[[2,3]]?

can be ambiguous.  I'm not so sure.  I think that $bignum is not
legal, under Perl5, to be an array index, and that if it is going to
be use so, it would need to be $array[$bugnum-value] anyway.

=head1 DESCRIPTION

Currently in Perl5, arrays can be indexed by a single integer, or sliced
by a list of integers. This notation is sufficient and convenient for one
dimensional arrays (like Perl5 has), but is problematic for
multidimensional arrays.

Because n-dimensional arrays have more than one dimension, their indices
are not simple integers, but rather n-tuples of integers. While lists are
natural Perl representations of n-tuples, they present problems when used
as multidimensional array indices:

=over 4

=item *

It is ambiguous if @a[@point1,@point2] is a 2-element slice of a
multidimensional array, or if it is a multi-element slice of a single
dimensional array.

=item *

Although $a[@point] is not as ambiguous (it is currently a syntax error),
it still could be confusing, especially when seeing both $a[1,3,4] and
@b[1,2,4].

=item *

It is inconvenient use a list of n-dimensional indices for a slice. By
Perl syntax, such a list would have to be a list of lists, whereas
currently Perl only allows lists of scalers.

=back

A solution to these problems is to encapsulate a list of integers into a
scaler, such as using a list reference. By using list references for
indices (and lists of list references for slices), the multidimensional
case is similar to the unidimensional case: $a[$scaler] would access a
single element, @a[@list] would access a slice.

This would solve the problems above:

=over 4

=item *

@a[$point1,$point2] is no longer ambiguous, but obviously a 2-element
slice from the array @a. The dimensionality of @a is not an issue with
this syntax.

=item *

Multidimensional and unidimensional array access both use $a[$point], so
$a[1,3,4] will not be seen. Instead, any confusion would have to be
between $a[[1,3,4]] and @b[1,2,4]. Since $a[[1,3,4]] is an array indexed
by a single scaler value (the listref [1,3,4]), whereas @b[1,2,4] is an
array indexed by a list of scaler values explains clearly (and possibly
redundantly) the use of $ and @ as prefixes.

=item *

List of lists are naturally stored as list of scalar references to lists.
As such, the natural representation of lists of lists ties in well with
taking multidimensional array slices:

for my $t (1..10) { push @points,[getx($t),gety($t),getz($t)]; } $point =
$points[3];

@array[@points] = 1 x @points; $array[$point] = 0;

=back

This proposed syntax extends to unidimensional arrays as well. $a[[$b]]
being an alternative way of saying $a[$b], when $b is an integer. $a[[]]
would return the (unique) value of the zero-dimensional array @a, not the
value of the scaler $a.

A listref used as an index must have the same or lower cardinality as the
dimensionality of the array (if the array is declared with ':bounds'-- see
RFC 203), and must have elements within the declared or implied bounds of
the array indexed.

my int @array :bounds(3,3,3); $array[[0,1,2]] = 5; # OK, within bounds.
$array[[0,1,4]] = 5; # error, out of bounds $array[[1,2,1,2]] = 5; #
error, out of bounds, too many dimensions

If the cardinality (c) of the index is lower than the dimensions (d) of
the array, then the innermost (n-d) dimensions of the array are returned:

my int @array :bounds(3,3,3); $array[[1,2]] = (1,2,3,4); # Sets the line
at (1,2) to (1,2,3,4)

When a listref is used to index a list of lists, the returned list
reference is automatically dereferenced:

my @array( [0,1],
[1,2]);
my @a = @array[[0]]; # Returns (0,1), _not_ [0,1]

The ; operator (defined in RFC 205) is designed to return lists of
list refs, to make for efficient indexing and slicing of
multidimensional arrays.

@points = ((1,2);(3,4);5); @points = ([1,3,5],[1,4,5],[2,3,5],[2,4,5])