On Tue, Apr 05, 2005 at 02:38:05PM +0200, Ingo Blechschmidt wrote:
: Hi,
:
: Trey Harris wrote:
: > In a message dated Mon, 4 Apr 2005, Ingo Blechschmidt writes:
: >> What does pick return on hashes? Does it return a random value or a
: >> random pair? (I suppose returning a pair is more useful.)
: >
: > I'd assume in all cases that pick returns an *alias*, and in the case
: > of hashes, an alias to the pair:
: >
: > # Add entropy to your hash
: > for 1..$entropy_thresshold {
: > %hash.pick.value *= rand $scribble_factor;
: > }
:
: I like that, too. So:
: my @array = <a b c d>;
: my $elem = @array.pick;
: $elem = "z"; # $elem now "z", @array unchanged
:
: my @array = <a b c d>;
: my $elem := @array.pick;
: $elem = "z"; # $elem now "z", @array changed
: # (any(@array) eq "z" now true)
But what should we call "pick without replacment"? .peck?
Unfortunately @array.=pick isn't what you want. It would reduce the
array to one element. On the other hand, if .pick takes an argument
saying how many to pick, then maybe
@array.=pick([EMAIL PROTECTED])
gives you a random shuffle. Unfortunately,
@array.=pick(@array - 1)
won't tell you which one it left out.
: Same for hashes:
: my %hash = (a => 1, b => 2);
: my $pair = %hash.pick;
: $pair = ...; # %hash unchanged
:
: 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. However, if you were to say,
my Pair $pair := %hash.pick;
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.
Larry