Hi,

Larry Wall wrote:
> : Same for hashes:
[...]
> :   my %hash = (a => 1, b => 2),
> :   my $pair := %hash.pick;
> :   $pair = ...; # %hash changed
> 
> I'm not sure that works.  We don't quite have pairs as first class
> containers.  Binding would try to use a pair as a named argument, and
> would fail unless the key happened to be 'pair', which it isn't in
> this case.

Oh yes, of course. Others may be interested in
http://groups.google.com/groups?threadm=x7wtsvo0fs.fsf%40mail.sysarch.com.

> then it has a better chance of working, presuming someone has the
> gumption to write .pick on hashes, which doesn't look entirely trivial
> to do right.

<thinking out loud>I'm sure I overlooked something, but the following
seems to be correct and is not *that* difficult :):
  class Hash;
  ...;
  method pick() is rw {
    # First pick a random key.
    my $key = .keys.pick;
    # Then return an appropriate Proxy object:
    return new Proxy:
      FETCH => {

Ok. While typing the C<{> here, I realized you were correct :)
It'd be reasonable simple if there was a .get_pair_by_key method
(which'd do appropriate binding and'd be C<is rw>):

  method pick() is rw {
    my $key   = .keys.pick;
    my $pair := .get_pair_by_key($key);
    return $pair;
  }


--Ingo

-- 
Linux, the choice of a GNU | Mathematicians practice absolute freedom.
generation on a dual AMD   | -- Henry Adams  
Athlon!                    |

Reply via email to