Mix Reference
Immutable 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.
sub mix #
sub mix(*@args --> Mix)
Returns Mix.
Creates a new Mix from @args.
method Bag #
method Bag (--> Bag:D)
Returns Bag:D.
Coerces the Mix to a Bag. The weights are convert to Int, which means the number of keys in the resulting Bag can be fewer than in the original Mix, if any of the weights are negative or truncate to zero.
method BagHash #
method BagHash (--> BagHash:D)
Returns BagHash:D.
Coerces the Mix to a BagHash. The weights are convert to Int, which means the number of keys in the resulting BagHash can be fewer than in the original Mix, if any of the weights are negative or truncate to zero.
method reverse #
Note: This method is inherited from Any, however, Mixes do not have an inherent order and you should not trust it returning a consistent output.
method total #
method total(Mix:D: --> Real)
Returns Real.
Returns the sum of all the weights
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.
1 all-differ · 1 doc-drift · 3 no-output · 4 ok · 1 rakudo-differs · 1 rakupp-differs
class Mix 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).Mix;
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).Mix;
say $recipe<butter>; # OUTPUT: «0.22»
say $recipe<sugar>; # OUTPUT: «0.12»
say $recipe<chocolate>; # OUTPUT: «0»0.22
0.12
0
Documentation, Rakudo and Raku++ all agree.
my $n = mix "a", "a", "b" => 0, 3.14, π, π; # The Pair is a single element say $n.keys.map: *.^name; # OUTPUT: «(Rat Pair Num Str)» say $n.pairs; # OUTPUT: «(3.14 => 1 (b => 0) => 1 3.141592653589793 => 2 a => 2)»
(Rat Pair Num Str)
(3.14 => 1 (b => 0) => 1 3.141592653589793 => 2 a => 2)
(Rat Str Num Pair)
(3.14 => 1 a => 2 3.141592653589793 => 2 (b => 0) => 1)
(Rat Num Str Pair)
(3.14 => 1 3.141592653589793 => 2 a => 2 (b => 0) => 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).Mix;
say $n.keys.map(&WHAT); # OUTPUT: «((Str) (Str))»
say $n.pairs; # OUTPUT: «(a => 2 c => 3.14)»((Str) (Str))
(a => 2 c => 3.14)
((Str) (Str))
(c => 3.14 a => 2)
((Str) (Str))
(a => 2 c => 3.14)
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 %n is Mix = ("a", "a", "b" => 0, "c" => 3.14);
say %n.^name; # OUTPUT: «Mix»
say %n; # OUTPUT: «Mix(a(2) c(3.14))»Mix
Mix(a(2) c(3.14))
Documentation, Rakudo and Raku++ all agree.
# only allow strings my $n = Mix[Str].new: <a b b c c c>;
Not executed: the documentation states no expected output for this example.
# only allow strings my %m is Mix[Str] = <a b b c c c>; say %m<b>; # OUTPUT: «2» say %m<d>; # OUTPUT: «0»
2
0
Documentation, Rakudo and Raku++ all agree.
# only allow whole numbers
my %m is Mix[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 $this-mix = (sugar => ⅓, spice => ¼, all-things-nice => ¾); my $that-mix = ( sugar => 1, spice => 2); say $that-mix (<) $this-mix; # OUTPUT: «True» say $that-mix (^) $this-mix; # OUTPUT: «Set(all-things-nice)» say $that-mix (+) $this-mix; # OUTPUT: «Bag(spice(2) sugar)» # Unicode versions: say $that-mix ⊂ $this-mix; # OUTPUT: «True» say $that-mix ⊖ $this-mix; # OUTPUT: «Set(all-things-nice)» say $that-mix ⊎ $this-mix; # OUTPUT: «Bag(spice(2) sugar)»
True
Set(all-things-nice)
Bag(spice(2) sugar)
True
Set(all-things-nice)
Bag(spice(2) sugar)
True
Set(all-things-nice)
Bag(spice(2) sugar)
True
Set(all-things-nice)
Bag(spice(2) sugar)
True
Set(all-things-nice)
Bag(all-things-nice(0.75) spice(2.25) sugar(1.3333333333333333))
True
Set(all-things-nice)
Bag(all-things-nice(0.75) spice(2.25) sugar(1.3333333333333333))
Raku++ disagrees with both the documentation and Rakudo — a defect.
say mix('a', 'b', 'c', 'a', 'a', 'd').total == 6; # OUTPUT: «True»
say %(a => 5.6, b => 2.4).Mix.total == 8; # OUTPUT: «True»True
True
Documentation, Rakudo and Raku++ all agree.