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

Re: Pair of lists => list of pairs?

9 views
Skip to first unread message

Gaal Yahas

unread,
Aug 23, 2006, 5:51:04 PM8/23/06
to Mark J. Reed, perl6-l...@perl.org
On Wed, Aug 23, 2006 at 05:43:48PM -0400, Mark J. Reed wrote:
> But is there an easy way in Perl6 to do it all in one go? Should this work?
>
> my %h = @k [=>] @v;

You want a zip:

my %h = @k ¥ @v;
my %h = @k Y @v; # ASCII fallback
my %h = zip(@k, @v); # or maybe zip(@k; @v) this week?

--
Gaal Yahas <ga...@forum2.org>
http://gaal.livejournal.com/

Mark J. Reed

unread,
Aug 23, 2006, 5:43:48 PM8/23/06
to perl6-l...@perl.org
Suppose I have two arrays @k and @v and I want to declare and initialize a
hash %h such that %h.keys eqv @k and %h.values eqv @v.

I could use a direct translation of the P5 idiom:

my %h;
%h{@k} = @v;

But is there an easy way in Perl6 to do it all in one go? Should this work?

my %h = @k [=>] @v;

--
Mark J. Reed <mark...@mail.com>

Larry Wall

unread,
Aug 23, 2006, 6:13:36 PM8/23/06
to perl6-l...@perl.org
On Wed, Aug 23, 2006 at 05:43:48PM -0400, Mark J. Reed wrote:
: Suppose I have two arrays @k and @v and I want to declare and initialize a

: hash %h such that %h.keys eqv @k and %h.values eqv @v.
:
: I could use a direct translation of the P5 idiom:
:
: my %h;
: %h{@k} = @v;
:
: But is there an easy way in Perl6 to do it all in one go? Should this work?
:
: my %h = @k [=>] @v;

Reduce operators only turn infix into list operators. What you really
want here is a hyper-fatarrow:

my %h = @k »=>« @v;

Larry

Juerd

unread,
Aug 23, 2006, 5:42:57 PM8/23/06
to perl6-l...@perl.org
Mark J. Reed skribis 2006-08-23 17:43 (-0400):

> But is there an easy way in Perl6 to do it all in one go? Should this work?
> my %h = @k [=>] @v;

Hyper is not [], but >><<. And >>=><< works perfectly in Pugs, and does
exactly what you describe.

[] is for reduction, and is prefix: [+] 1,2,3


Juerd
--
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html
http://convolution.nl/gajigu_juerd_n.html

Michael Snoyman

unread,
Aug 23, 2006, 6:16:39 PM8/23/06
to perl6-l...@perl.org
>
> : my %h;
> : %h{@k} = @v;
> :
> : But is there an easy way in Perl6 to do it all in one go? Should this
> work?
> :
> : my %h = @k [=>] @v;
>
> Reduce operators only turn infix into list operators. What you really
> want here is a hyper-fatarrow:
>
> my %h = @k »=>« @v;


Gaal pointed out using zip. What would be the difference then between a
hyper-fatarrow and zip in this case?


Michael

Larry Wall

unread,
Aug 23, 2006, 6:19:22 PM8/23/06
to perl6-l...@perl.org
On Thu, Aug 24, 2006 at 12:51:04AM +0300, Gaal Yahas wrote:

: On Wed, Aug 23, 2006 at 05:43:48PM -0400, Mark J. Reed wrote:
: > But is there an easy way in Perl6 to do it all in one go? Should this work?
: >
: > my %h = @k [=>] @v;
:
: You want a zip:
:
: my %h = @k ¥ @v;
: my %h = @k Y @v; # ASCII fallback

That would have worked back when zip merely interleaved, but now it makes
sublists, and so we would have to teach hash assignment to transform [$k, $v]
into ($k,$v) or $k=>$v. Might not be a bad idea.

: my %h = zip(@k, @v); # or maybe zip(@k; @v) this week?

It would be zip(@k;@v). zip(@k,@v) would only have one dimension, so
would just concatenate the two lists and put each element into its
own sublist.

Alternately, for the old zip semantics we have each(@k;@v), which
makes a list with interleaved keys and values. It's just there's no
operator like ¥ for it (yet).

But I'd still probably use a hyper-fatarrow for this case rather than
relying on interleaving.

Larry

Mark J. Reed

unread,
Aug 23, 2006, 6:24:14 PM8/23/06
to Michael Snoyman, perl6-l...@perl.org
>
> > Reduce operators only turn infix into list operators. What you really
> > want here is a hyper-fatarrow:
> >
> > my %h = @k »=>« @v;

Ah, right. Silly me. I got hyper and reduce confused. Thanks!


Gaal pointed out using zip. What would be the difference then between a
> hyper-fatarrow and zip in this case?


Effectively none. But I think the hyper-notation is clearer here. Both ¥
and => make pairs, but at least to me, => conveys more explicitly that it's
not just any old pair but specifically a key/value pair. (Even though =>
also creates Pairs of the "any old" kind.)

Larry Wall

unread,
Aug 23, 2006, 6:38:57 PM8/23/06
to perl6-l...@perl.org
On Wed, Aug 23, 2006 at 03:19:22PM -0700, Larry Wall wrote:
: But I'd still probably use a hyper-fatarrow for this case rather than
: relying on interleaving.

Another reason for preferring hyper is that it makes promises about
parallelizability, whereas the zip/each solutions would tend to
assume the input streams must be processed in order, albeit lazily.
(Probably doesn't make much difference until someone actually attempts
to vectorize Perl though, and even when that happens, it's not clear
what the exact sequence of events would be if you feed a lazy pipe
to a hyper...)

Larry

0 new messages