Skip to content

Instantly share code, notes, and snippets.

@kfly8
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfly8/f6a9b1e77053c83a5442 to your computer and use it in GitHub Desktop.
Save kfly8/f6a9b1e77053c83a5442 to your computer and use it in GitHub Desktop.
weighted_shuffle_list
use List::UtilsBy qw/weighted_shuffle_by/;
my @wlist = (
{ value => 'a', weight => 0 },
{ value => 'b', weight => 0 },
);
weighted_shuffle_by { $_->{weight} } @wlist;
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