develooper Front page | perl.perl6.language.data | Postings from September 2000

Re: RFC 207 (v1) Array: Efficient Array Loops

From:
Buddha Buck
Date:
September 11, 2000 07:21
Subject:
Re: RFC 207 (v1) Array: Efficient Array Loops
Message ID:
4.3.2.7.0.20000911100808.00aff280@armstrong.cse.buffalo.edu
Sorry, the mailer did something unexpected...

At 12:00 AM 9/12/00 +1100, Jeremy Howard wrote:
 >c.soeller@auckland.ac.nz wrote:
 >> Reading through the examples left me wondering about some
 >> technicalities:
 >>
 >> >   @t[|i;|j] = @a[|j;|i];  # transpose 2-d @a
 >>
 >> Written like this it would require that @a is exact 2-dim, i.e. it would
 >> not just swap the first two dims of any n-dim array? I suppose if I'd
 >> want that I'd write
 >>
 >>     @t[|i;|j;] = @a[|j;|i;]; # trailing ';' implies there might be
 >> trailing dims
 >>
 >Not necessary. Since arrays support all the syntax of a plain old list of
 >lists, and the |i syntax just creates an implicit loop, the example quoted
 >from the RFC will work with other sized arrays. In fact, if it was only 2d,
 >it would more properly be:
 >
 >  $t[|i;|j] = $a[|j;|i];  # transpose 2-d @a

I think it's fair to say that I goofed, and was probably inconsistent with 
my use of @ and $ in RFC207.  It was the hardest (conceptually) of the RFCs 
I wrote, and it was written late at night -- my thinking probably wasn't as 
clear as it could have been.  I know I delayed in writing it because it 
posed interesting challenges to syntax and semantics.

And although Jeremy is doing a good job at white-washing it, I didn't have 
a clear mind as to what to do with iterators over arrays of run-time 
determines dimensions.

I think what I was thinking was that |i would act as a list, therefore 
@t[|i] is syntactically an array slice, and therefore uses @, not $.  I'm 
not so certain that that makes a tremendous amount of sense.

The problem is that the isolated expression ~a[|i;|j] (using ~ as a 
placeholder for $ or @) evaluates to a 2D array, so should use @, but the 
|i;|j syntax is used to refer to a single element, so it's sort of a 
scalar, and should use $.

While something like "$t[|i;|j] = $a[|j;|i];" looks clearly like 
scalar-to-scaler assignment of 2-D array elements, something like "@p = 
$a[$k;|i]*$b[|i;$k];" isn't so clear.  Multiply two scalers to get an 
array?!?  In truth, it's assigning an array to an array, so there is no 
problem except appearances.

It may make sense to go with "$t[[|i,|j]] = $a[[|j,|i]];", especially if 
there was some way to treat the inner [|i,|j] "lists" as first class:

   $dimensions = make_iter_list(bounds(@t));
   $t[$dimensions] = $a[[reverse @$dimensions]];

As for c.soeller's other questions, I left them on a different computer, so 
I'll have to respond to them after I get home tonight (around 21:30 UTC).

 >With three dimensions, each implicit loop in
 >
 >  @t[|i;|j] = @a[|j;|i];  # transpose 2-d @a
 >
 >is assigning the _list_ (or 1d array) at @a[|j;|i] to the appropriate index
 >in @t. Ditto for >3 dimensions, except that it is a >1d array (or LOL) that
 >is being assigned at each index through the implicit loop.




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About