Baggy Reference
Collection of distinct weighted objects
What it is #
Position in the hierarchy #
Routines #
Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.
method new-from-pairs #
method new-from-pairs(Baggy: *@pairs --> Baggy:D)
Returns Baggy:D.
Constructs a Baggy objects from a list of Pair objects given as positional arguments: Note: be sure you aren't accidentally passing the Pairs as positional arguments; the quotes around the keys in the above example are significant.
method grab #
multi method grab(Baggy:D: --> Any)
multi method grab(Baggy:D: $count --> Seq:D)
Returns Any or Seq:D.
Like pick, a grab returns a random selection of elements, weighted by the values corresponding to each key. Unlike pick, it works only on mutable structures, e.g. BagHash. Use of grab on an immutable structure results in an X::Immutable exception. If * is passed as $count, or $count is greater than or equal to the total of the invocant, then total elements from the
method grabpairs #
multi method grabpairs(Baggy:D: --> Any)
multi method grabpairs(Baggy:D: $count --> Seq:D)
Returns Any or Seq:D.
Returns a Pair or a Seq of Pairs depending on the version of the method being invoked. Each Pair returned has an element of the invocant as its key and the element's weight as its value. Unlike pickpairs, it works only on mutable structures, e.g. BagHash. Use of grabpairs on an immutable structure results in
method pick #
multi method pick(Baggy:D: --> Any)
multi method pick(Baggy:D: $count --> Seq:D)
Returns Any or Seq:D.
Like an ordinary list pick, 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 pick used. The underlying metaphor for picking is that you're pulling colored marbles out a bag. (For "picking with replacement" see roll instead). If * is passed as $count, or $count is
method pickpairs #
multi method pickpairs(Baggy:D: --> Pair:D)
multi method pickpairs(Baggy:D: $count --> Seq:D)
Returns Pair:D or Seq:D.
Returns a Pair or a Seq of Pairs depending on the version of the method being invoked. Each Pair returned has an element of the invocant as its key and the element's weight as its value. The elements are 'picked' without replacement. If * is passed as $count, or $count is greater than or equal to the number of elements of the invocant, then all element/weight Pairs from
method roll #
multi method roll(Baggy:D: --> Any:D)
multi method roll(Baggy:D: $count --> Seq:D)
Returns Any:D or Seq:D.
Like an ordinary list 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 $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
method pairs #
method pairs(Baggy:D: --> Seq:D)
Returns Seq:D.
Returns all elements and their respective weights as a Seq of Pairs where the key is the element itself and the value is the weight of that element.
method antipairs #
method antipairs(Baggy:D: --> Seq:D)
Returns Seq:D.
Returns all elements and their respective weights as a Seq of Pairs, where the element itself is the value and the weight of that element is the key, i.e. the opposite of method pairs.
method invert #
method invert(Baggy:D: --> Seq:D)
Returns Seq:D.
Returns all elements and their respective weights as a Seq of Pairs, where the element itself is the value and the weight of that element is the key, i.e. the opposite of method pairs. Except for some esoteric cases, invert on a Baggy type returns the same result as antipairs.
method classify-list #
multi method classify-list(&mapper, *@list --> Baggy:D)
multi method classify-list(%mapper, *@list --> Baggy:D)
multi method classify-list(@mapper, *@list --> Baggy:D)
Returns Baggy:D.
Populates a mutable Baggy by classifying the possibly-empty @list of values using the given mapper. The @list cannot be lazy. The mapper can be a Callable that takes a single argument, an Associative, or an Iterable. With Associative and an Iterable mappers, the values in the @list represent the key and index of the mapper's
method categorize-list #
multi method categorize-list(&mapper, *@list --> Baggy:D)
multi method categorize-list(%mapper, *@list --> Baggy:D)
multi method categorize-list(@mapper, *@list --> Baggy:D)
Returns Baggy:D.
Populates a mutable Baggy by categorizing the possibly-empty @list of values using the given mapper. The @list cannot be lazy. The mapper can be a Callable that takes a single argument, an Associative, or an Iterable. With Associative and an Iterable mappers, the values in the @list represent the key and index of the mapper's
method keys #
method keys(Baggy:D: --> Seq:D)
Returns Seq:D.
Returns a Seq of all keys in the Baggy object without taking their individual weights into account as opposed to kxxv.
method values #
method values(Baggy:D: --> Seq:D)
Returns Seq:D.
Returns a Seq of all values, i.e. weights, in the Baggy object.
method kv #
method kv(Baggy:D: --> Seq:D)
Returns Seq:D.
Returns a Seq of keys and values interleaved.
method kxxv #
method kxxv(Baggy:D: --> Seq:D)
Returns Seq:D.
Returns a Seq of the keys of the invocant, with each key multiplied by its weight. Note that kxxv only works for Baggy types which have integer weights, i.e. Bag and BagHash.
method elems #
method elems(Baggy:D: --> Int:D)
Returns Int:D.
Returns the number of elements in the Baggy object without taking the individual elements' weight into account.
method total #
method total(Baggy:D:)
Returns the sum of weights for all elements in the Baggy object.
method default #
method default(Baggy:D: --> 0)
Returns 0.
Returns zero.
method hash #
method hash(Baggy:D: --> Hash:D)
Returns Hash:D.
Returns a Hash where the elements of the invocant are the keys and their respective weights the values.
method Bool #
method Bool(Baggy:D: --> Bool:D)
Returns Bool:D.
Returns True if the invocant contains at least one element.
method Set #
method Set(--> Set:D)
Returns Set:D.
Returns a Set whose elements are the keys of the invocant.
method SetHash #
method SetHash(--> SetHash:D)
Returns SetHash:D.
Returns a SetHash whose elements are the keys of the invocant.
method ACCEPTS #
method ACCEPTS($other --> Bool:D)
Returns Bool:D.
Used in smartmatching if the right-hand side is a Baggy. If the right-hand side is the type object, i.e. Baggy, the method returns True if $other does Baggy otherwise False is returned. If the right-hand side is a Baggy object, True is returned only if $other has the same elements, with the same weights, as the invocant. Sets, Bags, and Mixes
Examples, run three ways #
Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.
5 all-differ · 1 doc-drift · 5 no-output · 4 not-runnable · 21 ok · 1 rakudo-differs · 1 rakupp-differs
role Baggy does QuantHash { }Not executed: the documentation states no expected output for this example.
say Mix.new-from-pairs: 'butter' => 0.22, 'sugar' => 0.1, 'sugar' => 0.02; # OUTPUT: «Mix(butter(0.22) sugar(0.12))»
Mix(butter(0.22) sugar(0.12))
Documentation, Rakudo and Raku++ all agree.
my $cars = ('Ford' => 2, 'Rover' => 3).BagHash;
say $cars.grab; # OUTPUT: «Ford»
say $cars.grab(2); # OUTPUT: «(Rover Rover)»
say $cars.grab(*); # OUTPUT: «(Rover Ford)»Ford
(Rover Rover)
(Rover Ford)
Ford
(Rover Ford)
(Rover Rover)
Ford
(Rover Rover)
(Rover Ford)
Raku++ matches the documentation; Rakudo does not. This is the one class where neither engine can be assumed right: it may be a stale doc that Raku++ was built from, or it may be a Rakudo bug that the documentation predates. Each case is examined individually.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
my $breakfast = ('eggs' => 2, 'bacon' => 3).Bag;
say $breakfast.grab;
CATCH { default { put .^name, ': ', .Str } };
# OUTPUT: «X::Immutable: Cannot call 'grab' on an immutable 'Bag'»X::Immutable: Cannot call 'grab' on an immutable 'Bag'
Documentation, Rakudo and Raku++ all agree.
my $breakfast = (eggs => 2, bacon => 3).BagHash; say $breakfast.grabpairs; # OUTPUT: «bacon => 3» say $breakfast; # OUTPUT: «BagHash.new(eggs(2))» say $breakfast.grabpairs(1); # OUTPUT: «(eggs => 2)» say $breakfast.grabpairs(*); # OUTPUT: «()»
bacon => 3
BagHash.new(eggs(2))
(eggs => 2)
()
eggs => 2
BagHash(bacon(3))
(bacon => 3)
()
bacon => 3
BagHash(eggs(2))
(eggs => 2)
()
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
my $diet = ('eggs' => 2, 'bacon' => 3).Bag;
say $diet.grabpairs;
CATCH { default { put .^name, ': ', .Str } };
# OUTPUT: «X::Immutable: Cannot call 'grabpairs' on an immutable 'Bag'»X::Immutable: Cannot call 'grabpairs' on an immutable 'Bag'
Documentation, Rakudo and Raku++ all agree.
my $breakfast = bag <eggs bacon bacon bacon>; say $breakfast.pick; # OUTPUT: «eggs» say $breakfast.pick(2); # OUTPUT: «(eggs bacon)»
eggs
(eggs bacon)
bacon
(bacon eggs)
bacon
(eggs bacon)
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
say $breakfast.total; # OUTPUT: «4» say $breakfast.pick(*); # OUTPUT: «(bacon bacon bacon eggs)»
Neither engine can run this in isolation — the example depends on context from the surrounding text.
my $breakfast = bag <eggs bacon bacon bacon>; say $breakfast.pickpairs; # OUTPUT: «eggs => 1» say $breakfast.pickpairs(1); # OUTPUT: «(bacon => 3)» say $breakfast.pickpairs(*); # OUTPUT: «(eggs => 1 bacon => 3)»
eggs => 1
(bacon => 3)
(eggs => 1 bacon => 3)
bacon => 3
(eggs => 1)
(bacon => 3 eggs => 1)
bacon => 3
(eggs => 1)
(eggs => 1 bacon => 3)
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
my $breakfast = bag <eggs bacon bacon bacon>; say $breakfast.roll; # OUTPUT: «bacon» say $breakfast.roll(3); # OUTPUT: «(bacon eggs bacon)»
bacon
(bacon eggs bacon)
bacon
(bacon bacon bacon)
eggs
(eggs eggs bacon)
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
my $random_dishes := $breakfast.roll(*); say $random_dishes[^5]; # OUTPUT: «(bacon eggs bacon bacon bacon)»
Neither engine can run this in isolation — the example depends on context from the surrounding text.
my $breakfast = bag <bacon eggs bacon>; my $seq = $breakfast.pairs; say $seq.sort; # OUTPUT: «(bacon => 2 eggs => 1)»
(bacon => 2 eggs => 1)
Documentation, Rakudo and Raku++ all agree.
my $breakfast = bag <bacon eggs bacon>; my $seq = $breakfast.antipairs; say $seq.sort; # OUTPUT: «(1 => eggs 2 => bacon)»
(1 => eggs 2 => bacon)
Documentation, Rakudo and Raku++ all agree.
my $breakfast = bag <bacon eggs bacon>; my $seq = $breakfast.invert; say $seq.sort; # OUTPUT: «(1 => eggs 2 => bacon)»
(1 => eggs 2 => bacon)
Documentation, Rakudo and Raku++ all agree.
say BagHash.new.classify-list: { $_ %% 2 ?? 'even' !! 'odd' }, ^10;
# OUTPUT: BagHash(even(5) odd(5))Not executed: the documentation states no expected output for this example.
my @mapper = <zero one two three four five>; say MixHash.new.classify-list: @mapper, 1, 2, 3, 4, 4, 6; # OUTPUT: MixHash((Any) two three four(2) one)
Not executed: the documentation states no expected output for this example.
say BagHash.new.categorize-list: {
gather {
take 'largish' if $_ > 5;
take .is-prime ?? 'prime' !! 'non-prime';
take $_ %% 2 ?? 'even' !! 'odd';
}
}, ^10;
# OUTPUT: BagHash(largish(4) even(5) non-prime(6) prime(4) odd(5))Not executed: the documentation states no expected output for this example.
my %mapper = :sugar<sweet white>, :lemon<sour>, :cake('sweet', 'is-a-lie');
say MixHash.new.categorize-list: %mapper, <sugar lemon cake>;
# OUTPUT: MixHash(is-a-lie sour white sweet(2))Not executed: the documentation states no expected output for this example.
my $breakfast = bag <eggs spam spam spam>; say $breakfast.keys.sort; # OUTPUT: «(eggs spam)»
(eggs spam)
Documentation, Rakudo and Raku++ all agree.
my $n = ("a" => 5, "b" => 2).BagHash;
say $n.keys.sort; # OUTPUT: «(a b)»(a b)
Documentation, Rakudo and Raku++ all agree.
my $breakfast = bag <eggs spam spam spam>; say $breakfast.values.sort; # OUTPUT: «(1 3)»
(1 3)
Documentation, Rakudo and Raku++ all agree.
my $n = ("a" => 5, "b" => 2, "a" => 1).BagHash;
say $n.values.sort; # OUTPUT: «(2 6)»(2 6)
Documentation, Rakudo and Raku++ all agree.
my $breakfast = bag <eggs spam spam spam>; say $breakfast.kv; # OUTPUT: «(spam 3 eggs 1)»
(spam 3 eggs 1)
(eggs 1 spam 3)
(eggs 1 spam 3)
Both engines agree; the documentation states something else. Trust the engines.
my $n = ("a" => 5, "b" => 2, "a" => 1).BagHash;
say $n.kv; # OUTPUT: «(a 6 b 2)»(a 6 b 2)
Documentation, Rakudo and Raku++ all agree.
my $breakfast = bag <spam eggs spam spam bacon>; say $breakfast.kxxv.sort; # OUTPUT: «(bacon eggs spam spam spam)»
(bacon eggs spam spam spam)
Documentation, Rakudo and Raku++ all agree.
my $n = ("a" => 0, "b" => 1, "b" => 2).BagHash;
say $n.kxxv; # OUTPUT: «(b b b)»(b b b)
Documentation, Rakudo and Raku++ all agree.
my $breakfast = bag <eggs spam spam spam>; say $breakfast.elems; # OUTPUT: «2»
2
Documentation, Rakudo and Raku++ all agree.
my $n = ("b" => 9.4, "b" => 2).MixHash;
say $n.elems; # OUTPUT: «1»1
Documentation, Rakudo and Raku++ all agree.
my $breakfast = bag <eggs spam spam bacon>; say $breakfast.total; # OUTPUT: «4»
4
Documentation, Rakudo and Raku++ all agree.
my $n = ("a" => 5, "b" => 1, "b" => 2).BagHash;
say $n.total; # OUTPUT: «8»8
Documentation, Rakudo and Raku++ all agree.
my $breakfast = bag <eggs bacon>; say $breakfast.default; # OUTPUT: «0»
0
Documentation, Rakudo and Raku++ all agree.
my $breakfast = bag <eggs bacon bacon>;
my $h = $breakfast.hash;
say $h.^name; # OUTPUT: «Hash[Any,Any]»
say $h; # OUTPUT: «{bacon => 2, eggs => 1}»Hash[Any,Any]
{bacon => 2, eggs => 1}
Hash[UInt,Mu,Any]
{bacon => 2, eggs => 1}
Hash
{bacon => 2, eggs => 1}
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
my $breakfast = ('eggs' => 1).BagHash;
say $breakfast.Bool; # OUTPUT: «True»
# (since we have one element)
$breakfast<eggs> = 0; # weight == 0 will lead to element removal
say $breakfast.Bool; # OUTPUT: «False»True
False
Documentation, Rakudo and Raku++ all agree.
my $breakfast = (eggs => 2, bacon => 3).BagHash; say $breakfast.Set; # OUTPUT: «Set(bacon eggs)»
Set(bacon eggs)
Documentation, Rakudo and Raku++ all agree.
my $breakfast = (eggs => 2, bacon => 3).BagHash; my $sh = $breakfast.SetHash; say $sh.^name; # OUTPUT: «SetHash» say $sh.elems; # OUTPUT: «2»
SetHash
2
Documentation, Rakudo and Raku++ all agree.
my $breakfast = bag <eggs bacon>; say $breakfast ~~ Baggy; # OUTPUT: «True» say $breakfast.does(Baggy); # OUTPUT: «True»
True
True
True
True
True
False
Raku++ disagrees with both the documentation and Rakudo — a defect.
my $second-breakfast = (eggs => 1, bacon => 1).Mix; say $breakfast ~~ $second-breakfast; # OUTPUT: «True»
Neither engine can run this in isolation — the example depends on context from the surrounding text.
my $third-breakfast = (eggs => 1, bacon => 2).Bag; say $second-breakfast ~~ $third-breakfast; # OUTPUT: «False»
Neither engine can run this in isolation — the example depends on context from the surrounding text.