Created
October 10, 2015 16:06
-
-
Save lizmat/0530767dc0f53be6b7b5 to your computer and use it in GitHub Desktop.
Cannot find method 'compose'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/core/Mixy.pm b/src/core/Mixy.pm | |
index 4a09c38..9ee5338 100644 | |
--- a/src/core/Mixy.pm | |
+++ b/src/core/Mixy.pm | |
@@ -29,21 +29,10 @@ my role Mixy does Baggy { | |
fail ".pick is not supported on a {self.^name}"; | |
} | |
- multi method roll($count = 1) { | |
- my @pairs = self.pairs.grep: *.value > 0; | |
- my $total := [+] @pairs.map: *.value; | |
- my $rolls = nqp::istype($count,Num) | |
- ?? $total min $count !! nqp::istype($count,Whatever) ?? Inf !! $count; | |
- | |
- sub roll-one ($ignore?){ | |
- my $rand = $total.rand; | |
- my $seen = 0; | |
- for @pairs -> $pair { | |
- return $pair.key if ( $seen += $pair.value ) > $rand; | |
- } | |
- } | |
- | |
- map &roll-one, 1 .. $rolls; | |
+ multi method roll(Mixy:D:) { Rakudo::Internals.WeightedRoll(self).roll } | |
+ multi method roll(Mixy:D: $count) { | |
+ my $roller = Rakudo::Internals.WeightedRoll(self); | |
+ map { $roller.roll-one }, 1 .. $count; | |
} | |
} | |
diff --git a/src/core/Rakudo/Internals.pm b/src/core/Rakudo/Internals.pm | |
index f46acb7..fa0d464 100644 | |
--- a/src/core/Rakudo/Internals.pm | |
+++ b/src/core/Rakudo/Internals.pm | |
@@ -87,6 +87,31 @@ my class Rakudo::Internals { | |
nqp::getcurhllsym("@END_PHASERS")); | |
for @END -> $end { $end() }; | |
} | |
+ | |
+ method WeightedRoll(\mix) { | |
+ class { | |
+ has @!pairs; | |
+ has $!total; | |
+ | |
+ method BUILD(\mix) { | |
+ $!total = 0; | |
+ for mix.pairs { | |
+ my $value := .value; | |
+ if $value > 0 { | |
+ @!pairs.push($_); | |
+ $!total = $!total + $value; | |
+ } | |
+ } | |
+ self | |
+ } | |
+ method new(\mix) { nqp::create(self).BUILD(mix) } | |
+ method roll() { | |
+ my $rand = $!total.rand; | |
+ my $seen = 0; | |
+ return .key if ( $seen = $seen + .value ) > $rand for @!pairs; | |
+ } | |
+ }.new(mix) | |
+ } | |
} | |
# vim: ft=perl6 expandtab sw=4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment