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

Hyper-slices?

7 views
Skip to first unread message

David Christensen

unread,
Apr 17, 2005, 10:51:06 AM4/17/05
to perl6-l...@perl.org
Quick thought ---

Does the current design of Perl 6's hyper operators allow for
"hyper-slices"? I.e., if I want to model a matrix by using a list of
lists, is the following code valid/useful?

my @matrix=([1,2,3],[4,5,6],[7,8,9]);

my @row = @matrix[0]; # first row
my @col = @matrix>>[0]; #first column

my @transposed = @matrix>>[0..2];

I don't know if this case has been discussed earlier; is extracting an
element considered a unary operation? Is there some use for this
notation, other than what I just said?

I suppose the same could be said for Lists of Hashes:

my @records=({name=>"Tom",age=>27},{name=>"Dick",age=>33});

my @names= @records>>{'name'};
say @names;
# Tom Dick

Of course, for this to be useful, the lists would have to be
homogenous; @array is List of Hashes (excuse the syntax; I'm still
getting up to speed with a lot of the P6 specific issues).

Again, apologies if this is a closed domain/already has some other
method of retrieving the same information.

Thanks,

David Christensen

Luke Palmer

unread,
Apr 17, 2005, 6:17:34 PM4/17/05
to David Christensen, perl6-l...@perl.org
David Christensen writes:
> Quick thought ---
>
> Does the current design of Perl 6's hyper operators allow for
> "hyper-slices"? I.e., if I want to model a matrix by using a list of
> lists, is the following code valid/useful?
>
> my @matrix=([1,2,3],[4,5,6],[7,8,9]);
>
> my @row = @matrix[0]; # first row
> my @col = @matrix>>[0]; #first column

Almost.

my @col = @matrix».[0]

> my @transposed = @matrix>>[0..2];

The thing that makes me wonder about that is whether it builds anonymous
arrays inside for you, or whether it just flattens everything out.
Obviously in this case it *should* give you arrays.

On the other hand, if you did the standard map transform that hypers do:

my @transposed = map { $_[0..2] } @matrix

Then it's clearly flattening.

What I'm currently thinking is that Perl is terrible for matrix stuff.
Maybe we should keep it that way, and punt to PDL. But as it stands, we
have no hope of appeasing the mathematicians in this area without really
redoing hyper stuff. And I think that the hyper stuff is pretty close
to right for non-matrix purposes, so it would be hard to redo it without
perturbing what we have.

Luke

David Christensen

unread,
Apr 17, 2005, 7:20:54 PM4/17/05
to Luke Palmer, perl6-l...@perl.org
I definitely like the hyper stuff how it is; maybe the answer is to
just define an infix:<[[]]> operator which returns the crosswise slice
of a nested list of lists. In any case it could be shunted aside to
some package and certainly does not need to be in core.

David

Rod Adams

unread,
Apr 18, 2005, 3:02:58 AM4/18/05
to perl6-l...@perl.org
David Christensen wrote:

> I definitely like the hyper stuff how it is; maybe the answer is to
> just define an infix:<[[]]> operator which returns the crosswise slice
> of a nested list of lists. In any case it could be shunted aside to
> some package and certainly does not need to be in core.
>

Didn't S09 take care of that w/ the [ ; ; ] syntax?

-- Rod Adams

0 new messages