Skip to content

Commit

Permalink
Get .pick (ported from alpha, plus infinite versions) working.
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed Feb 19, 2010
1 parent 8fdc942 commit 3704a2e
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions src/core/Any-list.pm
Expand Up @@ -88,38 +88,36 @@ augment class Any {
@l[floor(@l.elems.rand)];
}

# multi method pick($num is copy = 1, :$replace) {
# if $num ~~ Whatever {
# $num = +Inf;
# }
# # $num .= floor;
#
# my @l = @.list.Seq;
#
# if ($num == 1) {
# return @l[floor(@l.elems.rand)];
# }
#
# if $replace {
# gather {
# while ($num > 0) {
# my $idx = floor(@l.elems.rand());
# take @l[$idx];
# --$num;
# }
# }
# } else {
# die "Non-replacing pick not yet implemented";
# # gather {
# # while ($num > 0 and @l.elems > 0) {
# # my $idx = floor(@l.elems.rand());
# # take @l[$idx];
# # @l.splice($idx,1);
# # --$num;
# # }
# # }
# }
# }
multi method pick($num is copy = 1, :$replace) {
my @l = @.list.Seq;

if ($num == 1) {
return @l[floor(@l.elems.rand)];
}

if $replace {
gather {
while ($num > 0) {
my $idx = floor(@l.elems.rand());
take @l[$idx];
--$num;
}
}
} else {
gather {
while ($num > 0 and @l.elems > 0) {
my $idx = floor(@l.elems.rand());
take @l[$idx];
@l.splice($idx,1);
--$num;
}
}
}
}

multi method pick(Whatever, :$replace) {
self.pick(Inf, :$replace);
}

multi method reduce(Code $expression is rw) {
my $arity = $expression.count;
Expand Down

0 comments on commit 3704a2e

Please sign in to comment.