does Baggy
A BagHash
is a mutable bag/multiset, meaning a collection of distinct items in no particular order that each have an integer weight assigned to them signifying how many copies of that element are considered "in the bag". If you do not need the mutability that a BagHash
provides, consider using the immutable Bag
type instead.
An item may be a definite object of any type – not just a Str
. For example, you can store Sub
's in a BagHash
, and you will store the actual Sub
rather than a string with the same name as the Sub
. Within a BagHash
, items that would compare positively with the === operator are considered the same element, with the number of how many there were as its weight. Alternatively, you can use the kxxv
method to easily get back the expanded list of items (without the order):
my = <spam eggs spam spam bacon spam>.BagHash;say .elems; # OUTPUT: «3»say .keys.sort; # OUTPUT: «bacon eggs spam»say .total; # OUTPUT: «6»say .kxxv.sort; # OUTPUT: «bacon eggs spam spam spam spam»
BagHash
es can be treated as object hashes using the { }
postcircumfix operator, or the < >
postcircumfix operator for literal string keys, which returns the corresponding integer weight for keys that are elements of the bag, and 0
for keys that aren't. These operators can also be used to modify weights (see Updating BagHash Objects, below).
my = <spam eggs spam spam bacon spam>.BagHash;say <bacon>; # OUTPUT: «1»say <spam>; # OUTPUT: «4»say <sausage>; # OUTPUT: «0»<sausage> = 2;<bacon>--;say .kxxv.sort; # OUTPUT: «eggs sausage sausage spam spam spam spam»
Creating BagHash
objects§
BagHash
es can be composed using BagHash.new
. Any positional parameters, regardless of their type, become elements of the bag:
my = BagHash.new: "a", "b", "c", "c";say .raku; # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash»say .keys.raku; # OUTPUT: «("b", "a", "c").Seq»say .values.raku; # OUTPUT: «(1, 1, 2).Seq»
Besides, BagHash.new-from-pairs
can create a BagHash
with items and their occurrences.
my = BagHash.new-from-pairs: "a" => 0, "b" => 1, "c" => 2, "c" => 2;say .raku; # OUTPUT: «("b"=>1,"c"=>4).BagHash»say .keys.raku; # OUTPUT: «("b", "c").Seq»say .values.raku; # OUTPUT: «(1, 4).Seq»
Alternatively, the .BagHash
coercer (or its functional form, BagHash()
) can be called on an existing object to coerce it to a BagHash
. Its semantics depend on the type and contents of the object. In general it evaluates the object in list context and creates a bag with the resulting items as elements, although for Hash-like objects or Pair items, only the keys become elements of the bag, and the (cumulative) values become the associated integer weights:
my = ("a", "b", "c", "c").BagHash;my = ("a" => 0, "b" => 1, "c" => 2, "c" => 2).BagHash;say .raku; # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash»say .raku; # OUTPUT: «("b"=>1,"c"=>4).BagHash»
You can also create BagHash
masquerading as a hash by using the is
trait:
my is BagHash = <a b b c c c>;say <b>; # OUTPUT: «2»say <d>; # OUTPUT: «0»
Since 6.d (2019.03 and later) it is also possible to specify the type of values you would like to allow in a BagHash
. This can either be done when calling .new
:
# only allow stringsmy = BagHash[Str].new: <a b b c c c>;
or using the masquerading syntax:
# only allow stringsmy is BagHash[Str] = <a b b c c c>;say <b>; # OUTPUT: «2»say <d>; # OUTPUT: «0»# only allow whole numbersmy is BagHash[Int] = <a b b c c c>;# Type check failed in binding; expected Int but got Str ("a")
Updating BagHash Objects§
Once you have created a BagHash
, you can update its values in two ways. First, you can use the add
and remove
methods:
my = BagHash.new: "a", "b", "c", "c";say .raku; # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash».add('c');say .raku; # OUTPUT: «("b"=>1,"c"=>3,"a"=>1).BagHash».remove(('b', 'a'),);say .raku; # OUTPUT: «("c"=>3).BagHash».remove('c');say .raku; # OUTPUT: «("c"=>2).BagHash»
Note that, as shown in the final example, the remove
method removes a single value from the BagHash
; it doesn't entirely remove the key from the BagHash
.
Alternatively, you can use assignment (including with autoincrement operators such as ++
and --
) to modify the BagHash
's contents.
my = BagHash.new: "a", "b", "c", "c";say .raku; # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash»<c>++;say .raku; # OUTPUT: «("b"=>1,"c"=>3,"a"=>1).BagHash»<b> -= 1;say .raku; # OUTPUT: «("a"=>1,"c"=>3).BagHash»= 0;say .raku; # OUTPUT: «("c"=>3).BagHash»
Using either syntax, if you set the value of an item to zero or less than zero, the item will be removed from the BagHash
.
Operators§
See Operators with set semantics for a complete list of "set operators" applicable to, among other types, BagHash
.
Examples:
my (, ) = BagHash.new(2, 2, 4), BagHash.new(2, 3, 3, 4);say (<) ; # OUTPUT: «False»say (<=) ; # OUTPUT: «False»say (^) ; # OUTPUT: «BagHash(3(2) 2)»say (+) ; # OUTPUT: «BagHash(2(3) 4(2) 3(2))»# Unicode versions:say ⊂ ; # OUTPUT: «False»say ⊆ ; # OUTPUT: «False»say ⊖ ; # OUTPUT: «BagHash(3(2) 2)»say ⊎ ; # OUTPUT: «BagHash(2(3) 4(2) 3(2))»
Note on reverse
and ordering.§
BagHash inherits reverse
from Any, however, Bag
s do not have an inherent order and you should not trust it returning a consistent output.
If you sort a BagHash, the result is a list of pairs, at which point reverse
makes perfect sense:
my = BagHash.new(2, 2, 18, 3, 4);say ; # OUTPUT: «BagHash(18 2(2) 3 4)»say .sort; # OUTPUT: «(2 => 2 3 => 1 4 => 1 18 => 1)»say .sort.reverse; # OUTPUT: «(18 => 1 4 => 1 3 => 1 2 => 2)»
method add§
method add(BagHash: \to-add, * --> 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 weight of each element by 1.
Note: Added in version 2020.02.
method remove§
method remove(BagHash: \to-remove, * --> 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 each element by 1 and removes any items with the resulting weight of 0.
Note: Added in version 2020.02.