MixHash Reference
Mutable collection of distinct objects with Real 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 Bag #
method Bag (--> Bag:D)
Returns Bag:D.
Coerces the MixHash to a Bag. The weights are converted to Int, which means the number of keys in the resulting Bag can be fewer than in the original MixHash, if any of the weights are negative or truncate to zero.
method BagHash #
method BagHash (--> BagHash:D)
Returns BagHash:D.
Coerces the MixHash to a BagHash. The weights are converted to Int, which means the number of keys in the resulting BagHash can be fewer than in the original MixHash, if any of the weights are negative or truncate to zero. 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.
2 all-differ · 2 doc-drift · 3 no-output · 3 ok
class MixHash does Mixy { }Not executed: the documentation states no expected output for this example.
my $recipe = (butter => 0.22, sugar => 0.1,
flour => 0.275, sugar => 0.02).MixHash;
say $recipe.elems; # OUTPUT: «3»
say $recipe.keys.sort; # OUTPUT: «butter flour sugar»
say $recipe.pairs.sort; # OUTPUT: «"butter" => 0.22 "flour" => 0.275 "sugar" => 0.12»
say $recipe.total; # OUTPUT: «0.615»3
butter flour sugar
"butter" => 0.22 "flour" => 0.275 "sugar" => 0.12
0.615
3
(butter flour sugar)
(butter => 0.22 flour => 0.275 sugar => 0.12)
0.615
3
(butter flour sugar)
(butter => 0.22 flour => 0.275 sugar => 0.12)
0.615
Both engines agree; the documentation states something else. Trust the engines.
my $recipe = (butter => 0.22, sugar => 0.1,
flour => 0.275, sugar => 0.02).MixHash;
say $recipe<butter>; # OUTPUT: «0.22»
say $recipe<sugar>; # OUTPUT: «0.12»
say $recipe<chocolate>; # OUTPUT: «0»
$recipe<butter> = 0;
$recipe<chocolate> = 0.30;
say $recipe.pairs; # OUTPUT: «"sugar" => 0.12 "flour" => 0.275 "chocolate" => 0.3»0.22
0.12
0
"sugar" => 0.12 "flour" => 0.275 "chocolate" => 0.3
0.22
0.12
0
(chocolate => 0.3 flour => 0.275 sugar => 0.12)
0.22
0.12
0
(chocolate => 0.3 flour => 0.275 sugar => 0.12)
Both engines agree; the documentation states something else. Trust the engines.
my $n = MixHash.new: "a", "a", "b" => 0, "c" => 3.14; say $n.keys.map(&WHAT); # OUTPUT: «((Str) (Pair) (Pair))» say $n.pairs; # OUTPUT: «(a => 2 (c => 3.14) => 1 (b => 0) => 1)»
((Str) (Pair) (Pair))
(a => 2 (c => 3.14) => 1 (b => 0) => 1)
((Pair) (Pair) (Str))
((c => 3.14) => 1 (b => 0) => 1 a => 2)
((Str) (Pair) (Pair))
(a => 2 (b => 0) => 1 (c => 3.14) => 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 $n = ("a", "a", "b" => 0, "c" => 3.14).MixHash;
say $n.keys.map(&WHAT); # OUTPUT: «((Str) (Str))»
say $n.pairs; # OUTPUT: «(a => 2 c => 3.14)»((Str) (Str))
(a => 2 c => 3.14)
Documentation, Rakudo and Raku++ all agree.
# only allow strings my $n = MixHash[Str].new: <a b b c c c>;
Not executed: the documentation states no expected output for this example.
# only allow strings my %mh is MixHash[Str] = <a b b c c c>; say %mh<b>; # OUTPUT: «2» say %mh<d>; # OUTPUT: «0»
2
0
Documentation, Rakudo and Raku++ all agree.
# only allow whole numbers
my %mh is MixHash[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 ($a, $b) = MixHash(2 => 2, 4), MixHash(2 => 1.5, 3 => 2, 4); say $a (<) $b; # OUTPUT: «False» say $a (<=) $b; # OUTPUT: «False» say $a (^) $b; # OUTPUT: «MixHash(2(0.5) 3(2))» say $a (+) $b; # OUTPUT: «MixHash(2(3.5) 4(2) 3(2))» # Unicode versions: say $a ⊂ $b; # OUTPUT: «False» say $a ⊆ $b; # OUTPUT: «False» say $a ⊖ $b; # OUTPUT: «MixHash(2(0.5) 3(2))» say $a ⊎ $b; # OUTPUT: «MixHash(2(3.5) 4(2) 3(2))»
False
False
MixHash(2(0.5) 3(2))
MixHash(2(3.5) 4(2) 3(2))
False
False
MixHash(2(0.5) 3(2))
MixHash(2(3.5) 4(2) 3(2))
False
False
MixHash(2(0.5) 3(2))
MixHash(2(3.5) 3(2) 4(2))
False
False
MixHash(2(0.5) 3(2))
MixHash(2(3.5) 3(2) 4(2))
False
False
Mix(2(0.5) 3(2))
Mix(2(3.5) 3(2) 4(2))
False
False
Mix(2(0.5) 3(2))
Mix(2(3.5) 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 = MixHash.new(2, 2, 18, 3, 4); say $a; # OUTPUT: «MixHash(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)»
MixHash(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.