Rules / Types, classes & roles / composite

BagHash Reference

Mutable collection of distinct objects with integer weights

What it is #

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 add #

method add(BagHash: \to-add, *%_ --> Nil)

Returns Nil.

When to-add is a single item, add inserts it into the BagHash or, if it was already present, increases its weight by 1. When to-add is a List, Array, Seq, or any other type that does the Iterator Role, add inserts each element of the Iterator into the SetHash or increments the

method remove #

method remove(BagHash: \to-remove, *%_ --> Nil)

Returns Nil.

When to-remove is a single item, remove reduces the weight of that item by one. If this results in the item having a weight of 0, this removes the item from the BagHash. If the item is not present in the BagHash, remove has no effect. When to-remove is a List, Array, Seq, or any other type that does the Iterator Role, remove reduces the weight of

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.

4 all-differ · 2 doc-drift · 3 no-output · 4 ok · 1 rakupp-differs

class BagHash does Baggy { }

Not executed: the documentation states no expected output for this example.

my $breakfast = <spam eggs spam spam bacon spam>.BagHash;

say $breakfast.elems;      # OUTPUT: «3␤»
say $breakfast.keys.sort;  # OUTPUT: «bacon eggs spam␤»

say $breakfast.total;      # OUTPUT: «6␤»
say $breakfast.kxxv.sort;  # OUTPUT: «bacon eggs spam spam spam spam␤»
Documentation
3
bacon eggs spam
6
bacon eggs spam spam spam spam
Rakudo
3
(bacon eggs spam)
6
(bacon eggs spam spam spam spam)
Raku++
3
(bacon eggs spam)
6
(bacon eggs spam spam spam spam)

Both engines agree; the documentation states something else. Trust the engines.

my $breakfast = <spam eggs spam spam bacon spam>.BagHash;
say $breakfast<bacon>;     # OUTPUT: «1␤»
say $breakfast<spam>;      # OUTPUT: «4␤»
say $breakfast<sausage>;   # OUTPUT: «0␤»

$breakfast<sausage> = 2;
$breakfast<bacon>--;
say $breakfast.kxxv.sort;  # OUTPUT: «eggs sausage sausage spam spam spam spam␤»
Documentation
1
4
0
eggs sausage sausage spam spam spam spam
Rakudo
1
4
0
(eggs sausage sausage spam spam spam spam)
Raku++
1
4
0
(eggs sausage sausage spam spam spam spam)

Both engines agree; the documentation states something else. Trust the engines.

my $n = BagHash.new: "a", "b", "c", "c";
say $n.raku;             # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤»
say $n.keys.raku;        # OUTPUT: «("b", "a", "c").Seq␤»
say $n.values.raku;      # OUTPUT: «(1, 1, 2).Seq␤»
Documentation
("b"=>1,"a"=>1,"c"=>2).BagHash
("b", "a", "c").Seq
(1, 1, 2).Seq
Rakudo
("b"=>1,"a"=>1,"c"=>2).BagHash
("b", "a", "c").Seq
(1, 1, 2).Seq
Raku++
("a"=>1,"b"=>1,"c"=>2).BagHash
("a", "b", "c").Seq
(1, 1, 2).Seq

Raku++ disagrees with both the documentation and Rakudo — a defect.

my $n = BagHash.new-from-pairs: "a" => 0, "b" => 1, "c" => 2, "c" => 2;
say $n.raku;             # OUTPUT: «("b"=>1,"c"=>4).BagHash␤»
say $n.keys.raku;        # OUTPUT: «("b", "c").Seq␤»
say $n.values.raku;      # OUTPUT: «(1, 4).Seq␤»
Output
("b"=>1,"c"=>4).BagHash
("b", "c").Seq
(1, 4).Seq

Documentation, Rakudo and Raku++ all agree.

my $m = ("a", "b", "c", "c").BagHash;
my $n = ("a" => 0, "b" => 1, "c" => 2, "c" => 2).BagHash;
say $m.raku;             # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤»
say $n.raku;             # OUTPUT: «("b"=>1,"c"=>4).BagHash␤»
Documentation
("b"=>1,"a"=>1,"c"=>2).BagHash
("b"=>1,"c"=>4).BagHash
Rakudo
("b"=>1,"c"=>2,"a"=>1).BagHash
("c"=>4,"b"=>1).BagHash
Raku++
("a"=>1,"b"=>1,"c"=>2).BagHash
("b"=>1,"c"=>4).BagHash

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 %bh is BagHash = <a b b c c c>;
say %bh<b>;  # OUTPUT: «2␤»
say %bh<d>;  # OUTPUT: «0␤»
Output
2
0

Documentation, Rakudo and Raku++ all agree.

# only allow strings
my $n = BagHash[Str].new: <a b b c c c>;

Not executed: the documentation states no expected output for this example.

# only allow strings
my %bh is BagHash[Str] = <a b b c c c>;
say %bh<b>;  # OUTPUT: «2␤»
say %bh<d>;  # OUTPUT: «0␤»
Output
2
0

Documentation, Rakudo and Raku++ all agree.

# only allow whole numbers
my %bh is BagHash[Int] = <a b b c c c>;
# Type check failed in binding; expected Int but got Str ("a")

Not executed: the documentation states no expected output for this example.

my $n = BagHash.new: "a", "b", "c", "c";
say $n.raku;             # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤»
$n.add('c');
say $n.raku;             # OUTPUT: «("b"=>1,"c"=>3,"a"=>1).BagHash␤»
$n.remove(('b', 'a'),);
say $n.raku;             # OUTPUT: «("c"=>3).BagHash␤»
$n.remove('c');
say $n.raku;             # OUTPUT: «("c"=>2).BagHash␤»
Documentation
("b"=>1,"a"=>1,"c"=>2).BagHash
("b"=>1,"c"=>3,"a"=>1).BagHash
("c"=>3).BagHash
("c"=>2).BagHash
Rakudo
("c"=>2,"a"=>1,"b"=>1).BagHash
("c"=>3,"a"=>1,"b"=>1).BagHash
("c"=>3).BagHash
("c"=>2).BagHash
Raku++
("a"=>1,"b"=>1,"c"=>2).BagHash
("a"=>1,"b"=>1,"c"=>2).BagHash

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 $n = BagHash.new: "a", "b", "c", "c";
say $n.raku;             # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤»
$n<c>++;
say $n.raku;             # OUTPUT: «("b"=>1,"c"=>3,"a"=>1).BagHash␤»
$n<b> -= 1;
say $n.raku;             # OUTPUT: «("a"=>1,"c"=>3).BagHash␤»
$n{'a'} = 0;
say $n.raku;             # OUTPUT: «("c"=>3).BagHash␤»
Documentation
("b"=>1,"a"=>1,"c"=>2).BagHash
("b"=>1,"c"=>3,"a"=>1).BagHash
("a"=>1,"c"=>3).BagHash
("c"=>3).BagHash
Rakudo
("a"=>1,"c"=>2,"b"=>1).BagHash
("a"=>1,"c"=>3,"b"=>1).BagHash
("a"=>1,"c"=>3).BagHash
("c"=>3).BagHash
Raku++
("a"=>1,"b"=>1,"c"=>2).BagHash
("a"=>1,"b"=>1,"c"=>3).BagHash
("a"=>1,"b"=>0,"c"=>3).BagHash
("b"=>0,"c"=>3).BagHash

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 ($a, $b) = BagHash.new(2, 2, 4), BagHash.new(2, 3, 3, 4);

say $a (<) $b;   # OUTPUT: «False␤»
say $a (<=) $b;  # OUTPUT: «False␤»
say $a (^) $b;   # OUTPUT: «BagHash(3(2) 2)␤»
say $a (+) $b;   # OUTPUT: «BagHash(2(3) 4(2) 3(2))␤»

# Unicode versions:
say $a ⊂ $b;  # OUTPUT: «False␤»
say $a ⊆ $b;  # OUTPUT: «False␤»
say $a ⊖ $b;  # OUTPUT: «BagHash(3(2) 2)␤»
say $a ⊎ $b;  # OUTPUT: «BagHash(2(3) 4(2) 3(2))␤»
Documentation
False
False
BagHash(3(2) 2)
BagHash(2(3) 4(2) 3(2))
False
False
BagHash(3(2) 2)
BagHash(2(3) 4(2) 3(2))
Rakudo
False
False
BagHash(2 3(2))
BagHash(2(3) 3(2) 4(2))
False
False
BagHash(2 3(2))
BagHash(2(3) 3(2) 4(2))
Raku++
False
False
Bag(2 3(2))
Bag(2(3) 3(2) 4(2))
False
False
Bag(2 3(2))
Bag(2(3) 3(2) 4(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 $a = BagHash.new(2, 2, 18, 3, 4);
say $a;  # OUTPUT: «BagHash(18 2(2) 3 4)␤»

say $a.sort;  # OUTPUT: «(2 => 2 3 => 1 4 => 1 18 => 1)␤»
say $a.sort.reverse;  # OUTPUT: «(18 => 1 4 => 1 3 => 1 2 => 2)␤»
Output
BagHash(18 2(2) 3 4)
(2 => 2 3 => 1 4 => 1 18 => 1)
(18 => 1 4 => 1 3 => 1 2 => 2)

Documentation, Rakudo and Raku++ all agree.