Ingo Blechschmidt <iblech <at> web.de> writes: > > 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; > } well :) # Ignoring multi-dimensionality method pairbykey($key is copy) is rw { return new Proxy: FETCH => -> $pair { $self{$key} }, STORE => -> $pair { my ($newkey, $newval) = $pair.kv; $self.delete($key); # Delete old entry $self{$key = $newkey} = $newval; # Add new entry }; } # Correct? BTW, this would make the following work, too: for %hash.pairs -> $pair is rw { $pair = ... } # Implementation of Hash::pairs: method pairs (Any|Junction [EMAIL PROTECTED]) { my @keys = @keytests ?? .keys.grep:{ $_ ~~ any @keytests } :: .keys; return .pairbykeyÂ.(@keys); } # Correct? The next logical step to full rw-ness would be a rw .pairs (@keytests removed for simplicity): method pairs ($self:) is rw { return new Proxy: FETCH => { .pairbykeyÂ.(.keys) }, STORE => -> [EMAIL PROTECTED] { .delete($_) for .keys; # [Is there a .clear? The obvious way, %hash = ()/ # $self = {} won't work of course.] $self{$_.key} = $_.value for @new; }; } # Correct? </thinking out loud> --Ingo -- Linux, the choice of a GNU | Mr. Cole's Axiom: The sum of the generation on a dual AMD | intelligence on the planet is a constant; Athlon! | the population is growing.