Skip to content

Instantly share code, notes, and snippets.

@dogbert17
Last active June 11, 2016 15:21
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 dogbert17/cb137b3fa751b853bda38f5750540ce0 to your computer and use it in GitHub Desktop.
Save dogbert17/cb137b3fa751b853bda38f5750540ce0 to your computer and use it in GitHub Desktop.
Attempt to document Baggy.roll. Most of the text is taken directly from https://design.perl6.org/S32/Containers.html#Bag
=head2 method roll
Defined as:
multi method roll(Baggy:D:) returns Any:D
multi method roll(Baggy:D: $count) returns Seq:D
Like an ordinary list L<roll|/type/List/#routine_roll>, but returns keys of the invocant weighted
by their values, as if the keys were replicated the number of times indicated
by the corresponding value and then list roll used. The underlying
metaphor for rolling is that you're throwing C<$count> dice that are
independent of each other, which (in bag terms) is equivalent to picking
a colored marble out your bag and then putting it back, and doing this
C<$count> times. In dice terms, the number of marbles corresponds to the
number of sides, and the number of marbles of the same color corresponds
to the number of sides with the same color. (For "picking without replacement"
see L<pick|#method pick> instead).
If C<*> is passed to C<$count>, returns a lazy, infinite sequence of randomly
chosen elements from the invocant.
my $breakfast = bag <eggs bacon bacon bacon>;
say $breakfast.roll; # bacon
say $breakfast.roll(3); # (bacon eggs bacon)
my $random_dishes := $breakfast.roll(*);
say $random_dishes[^5]; # (bacon eggs bacon bacon bacon)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment