Last active
August 29, 2015 14:04
-
-
Save kfly8/f6a9b1e77053c83a5442 to your computer and use it in GitHub Desktop.
weighted_shuffle_list
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
use List::UtilsBy qw/weighted_shuffle_by/; | |
my @wlist = ( | |
{ value => 'a', weight => 0 }, | |
{ value => 'b', weight => 0 }, | |
); | |
weighted_shuffle_by { $_->{weight} } @wlist; |
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
sub weighted_shuffle_by(&@) { | |
my $code = shift; | |
map { $_->{value} } | |
sort { $b->{rand} <=> $a->{rand} } | |
map { | |
+{ | |
rand => rand $code->($_) // 0, | |
value => $_, | |
} | |
} @_ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment