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

Pop a Hash?

175 views
Skip to first unread message

Rod Adams

unread,
Feb 9, 2005, 6:13:56 AM2/9/05
to Perl6 Language List

Does

($k, $v) <== pop %hash;
or
($k, $v) <== %hash.pop;


make sense to anyone except me?

Since we now have an explicit concept of pairs, one could consider a
hash to be nothing but an unordered (but well indexed) list of pairs.
So, C<< pop %hash >> would be a lot like C<< each >>, except, of course,
that it deletes the pair at the same time.

If we do that, I'd also want to be able to

push %x, %y;

which would mean something like:

%x{%y.keys} <== %y{%y.keys};

but be much easier to read.


-- Rod Adams.

(And now I'm really off to bed.)

Matthew Walton

unread,
Feb 9, 2005, 7:42:47 AM2/9/05
to Rod Adams, Perl6 Language List
Rod Adams wrote:
>
> Does
>
> ($k, $v) <== pop %hash;
> or
> ($k, $v) <== %hash.pop;
>
>
> make sense to anyone except me?

Makes sense to me. Although I would be more inclined to think of pop as
returning a pair - but does a pair in list context turn into a list of
key, value? If so then the above makes lots of sense.

> Since we now have an explicit concept of pairs, one could consider a
> hash to be nothing but an unordered (but well indexed) list of pairs.
> So, C<< pop %hash >> would be a lot like C<< each >>, except, of course,
> that it deletes the pair at the same time.

The only thing people might not be too pleased about is that the order
is entirely at the whim of the internal implementation of hashes. I
suppose you could say %hash.sort.pop(), but that would probably re-sort
every time and that's clearly not desirable. Working on a sorted copy is
also not particularly pleasant as memory could be a considerable problem.

> If we do that, I'd also want to be able to
>
> push %x, %y;
>
> which would mean something like:
>
> %x{%y.keys} <== %y{%y.keys};
>
> but be much easier to read.

Yes, I'd like that. I find myself wanting to do things like that quite a
lot in Perl 5.

Eirik Berg Hanssen

unread,
Feb 9, 2005, 8:18:06 AM2/9/05
to Matthew Walton, Rod Adams, Perl6 Language List
Matthew Walton <mat...@alledora.co.uk> writes:

> Rod Adams wrote:
>> Does
>> ($k, $v) <== pop %hash;
>> or
>> ($k, $v) <== %hash.pop;
>> make sense to anyone except me?
>
> Makes sense to me. Although I would be more inclined to think of pop
> as returning a pair - but does a pair in list context turn into a list
> of key, value? If so then the above makes lots of sense.

Seeing the above, I first thought up a "pair context":

($k => $v) = %hash.pop;

However ... IIRC, in list assignment, lvalue pairs are treated as
name/value pairs, so there would be a list assignment of just one
element here. And this is still list assignment ...

Better to be explicit, I suppose:

($k, $v) = %hash.pop.kv;


>> If we do that, I'd also want to be able to
>> push %x, %y;
>> which would mean something like:
>> %x{%y.keys} <== %y{%y.keys};
>> but be much easier to read.
>
> Yes, I'd like that. I find myself wanting to do things like that quite
> a lot in Perl 5.

%x.push(%y);

If no one else writes it, I will. ;-)


Eirik
--
A free society is one where it is safe to be unpopular.
-- Adlai Stevenson

David Storrs

unread,
Feb 10, 2005, 11:59:04 AM2/10/05
to Perl6 Language List
On Wed, Feb 09, 2005 at 05:13:56AM -0600, Rod Adams wrote:
>
> Does
>
> ($k, $v) <== pop %hash;
> or
> ($k, $v) <== %hash.pop;
>
> make sense to anyone except me?

It's clear to me.

The only thing is that, right off the top of my head, I can't see
where it would be used. Since the order in which the pairs were
returned would be effectively random, you can't use this to remove and
process a specific element. So, really, the only time it's useful is
if you want to process all the elements in the hash--in which case,
why not just use C< each >?

--Dks
--
dst...@dstorrs.com

Ashley Winters

unread,
Feb 11, 2005, 8:33:29 PM2/11/05
to Perl6 Language List
On Thu, 10 Feb 2005 08:59:04 -0800, David Storrs <dst...@dstorrs.com> wrote:
> On Wed, Feb 09, 2005 at 05:13:56AM -0600, Rod Adams wrote:
> >
> > Does
> >
> > ($k, $v) <== pop %hash;
> > or
> > ($k, $v) <== %hash.pop;
> >
> > make sense to anyone except me?
>
> ... the only time it's useful is

> if you want to process all the elements in the hash--in which case,
> why not just use C< each >?

That's true for the PerlHash implementation, but what about
OrderedHash? Or LookupQueue? Still seems like a useful trick.

Ashley

David Storrs

unread,
Feb 13, 2005, 2:14:09 PM2/13/05
to Perl6 Language List
On Fri, Feb 11, 2005 at 05:33:29PM -0800, Ashley Winters wrote:
> On Thu, 10 Feb 2005 08:59:04 -0800, David Storrs <dst...@dstorrs.com> wrote:
> > On Wed, Feb 09, 2005 at 05:13:56AM -0600, Rod Adams wrote:
> > >
> > > ($k, $v) <== pop %hash;
> > > make sense to anyone except me?
> >
> > ... the only time it's useful is
> > if you want to process all the elements in the hash--in which case,
> > why not just use C< each >?
>
> That's true for the PerlHash implementation, but what about
> OrderedHash? Or LookupQueue? Still seems like a useful trick.

Good point. I'm glad you pointed that out, because it felt like this
should be useful...glad to see it was just my imagination that was at
fault. :>

--Dks

--
dst...@dstorrs.com

0 new messages