# I want to shake an array, so its elements are ordered randomly.
#
# I discovered, that the behaviour of "delete" is odd and
#
#        something i can't discribe.
#
# Something with "gather". I know it is not finished, but I like to use it and...
# huh... can't understand that bug below.

# function: remove
# XXX # needed cause "delete @a, $n" only do "@a[$n] = undef".
# but "delete %a, $n" really deletes the pair.
multi remove (@rray is rw, $n)
{
   my $ret = @rray[$n];
   @rray = @rray[0..($n-1)],@rray[($n+1)..(@rray.end)];
   $ret;
}

multi steal (@rray is rw)
{
   remove @rray, pick(0..(@rray.end));
}

multi shake (@rray is copy)
{
   gather { take steal @rray while @rray }
}

### example ###

my @a = 0,1,2,3,4;

remove @a, 2;
# returns 2
# @a = 0,1,3,4

steal @a;
# returns maybe 1
# @a = 0,3,4

shake @a;
# XXX # don't do what it should
# it should return maybe 3,0,4
# but on my pugs it says:
# (#<Array:0x*******>,)

my @b = shake @a;
# returns (#<Scalar:0x*******>,)
# @b = (#<Array:0x*******>,)
# @b[0] = 0,3,4 ### always ###

# but watch this:

my @b = @a;
my @c = gather { take steal @b while @b } # exactly the same like "shake"
# @b = ()
# @c = 3,0,4
# a nice shake!

Reply via email to