Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add method form of rand, pick.
  • Loading branch information
colomon committed Oct 13, 2011
1 parent f5298c8 commit 82a2206
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/CORE.setting
Expand Up @@ -134,6 +134,24 @@ my class Any is Mu {
}
method hash() { anon %hash = @(self) }
multi method pick($num is copy = 1) {
my @l = @(self);
if ($num == 1) {
return @l[floor(@l.elems.rand)];
}
my $number-elements = @l.elems;
gather {
while ($num > 0 and $number-elements > 0) {
my $idx = floor($number-elements.rand());
take @l[$idx];
@l[$idx] = @l[--$number-elements];
--$num;
}
}
}
}
my class Cursor { ... }
Expand Down Expand Up @@ -184,6 +202,7 @@ my class Cool {
method cotanh() { Q:CgOp { (cotanh {self}) } }
method acotanh() { Q:CgOp { (acotanh {self}) } }
method atan2($x = 1) { Q:CgOp { (atan2 {self} {$x}) } }
method rand() { self * rand; }
method split($matcher, $limit?, :$all?) {
my $matchrx = (($matcher ~~ Regex) ?? $matcher !! /$matcher/);
Expand Down Expand Up @@ -2119,6 +2138,8 @@ sub cotanh($x) { $x.cotanh }
sub acotanh($x) { $x.acotanh }
sub atan2($y, $x = 1) { $y.atan2($x) }
sub pick($num, *@values) { @values.pick($num) }
INIT {
$PROCESS::IN ::= Q:CgOp { (box TextReader (treader_stdin)) };
$PROCESS::OUT ::= TextWriter.new;
Expand Down

0 comments on commit 82a2206

Please sign in to comment.