Collection of distinct objects
does QuantHash
A role for collections which make sure that each element can only appear once. See Set and SetHash.
method new-from-pairs(* --> Setty)
Constructs a Setty object from a list of Pair
objects given as positional arguments:
say Set.new-from-pairs: 'butter' => 0.22, 'salt' => 0, 'sugar' => 0.02;# OUTPUT: «Set(butter sugar)»
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( = 1)
Removes and returns $count
elements chosen at random (without repetition) from the set.
If *
is passed as $count
, or $count
is greater than or equal to the size of the set, then all its elements are removed and returned in random order.
Only works on mutable sets; When used on an immutable set, it results in an exception.
method grabpairs( = 1)
Removes $count
elements chosen at random (without repetition) from the set, and returns a list of Pair
objects whose keys are the grabbed elements and whose values are True
.
If *
is passed as $count
, or $count
is greater than or equal to the size of the set, then all its elements are removed and returned as Pair
s in the aforementioned way in random order.
Only works on mutable sets; When used on an immutable set, it results in an exception.
multi method pick( = 1)
Returns $count
elements chosen at random (without repetition) from the set.
If *
is passed as $count
, or $count
is greater than or equal to the size of the set, then all its elements are returned in random order (shuffled).
multi method pickpairs(Setty: --> Pair)multi method pickpairs(Setty: --> Seq)
Returns a Pair
or a Seq
of Pair
s depending on the candidate of the method being invoked. Each Pair
returned has an element of the invocant as its key and True
as its value. In contrast to grabpairs, 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/True
Pair
s from the invocant are returned in a random sequence; i.e. they are returned shuffled;
Note that each pickpairs
invocation maintains its own private state and has no effect on subsequent pickpairs
invocations.
my = set (4, 2, 3);say .pickpairs; # OUTPUT: «4 => True»say .pickpairs(1); # OUTPUT: «(3 => True)»say .pickpairs(*); # OUTPUT: «(2 => True 4 => True 3 => True)»
multi method roll( = 1)
Returns a lazy list of $count
elements, each randomly selected from the set. Each random choice is made independently, like a separate die roll where each die face is a set element.
If *
is passed as $count
, the list is infinite.
multi method antipairs(Setty: --> Seq)
Returns all elements in the set and True
as a Seq of Pair
objects, where the element itself is the value, i.e. the opposite of method pairs
.
my = Set.new(1, 2, 3, 1);say .antipairs.sort; # OUTPUT: «(True => 1 True => 2 True => 3)»
multi method keys(Setty: --> Seq)
Returns a Seq of all elements of the set.
my = Set.new(1, 2, 3);say .keys; # OUTPUT: «(3 1 2)»
multi method values(Setty: --> Seq)
Returns a Seq containing as many True
values as the set has elements.
my = Set.new(1, 2, 3);say .values; # OUTPUT: «(True True True)»
multi method kv(Setty: --> Seq)
Returns a Seq of the set's elements and True
values interleaved.
my = Set.new(1, 2, 3);say .kv; # OUTPUT: «(3 True 1 True 2 True)»
method elems(Setty: --> Int)
The number of elements of the set.
method total(Setty: --> Int)
The total of all the values of the QuantHash
object. For a Setty
object, this is just the number of elements.
multi method minpairs(Setty: --> Seq)
Returns the value of self.pairs
(as all Pairs have minimum values). See also Any.minpairs
multi method maxpairs(Setty: --> Seq)
Returns the value of self.pairs
(as all Pairs have maximum values). See also Any.maxpairs
method default(--> False)
Returns the default value of the invocant, i.e. the value which is returned when trying to access an element in the Setty
object which has not been previously initialized or when accessing an element which has explicitly been set to Nil
or False
.
my = SetHash.new(1, 2, 3);say ; # OUTPUT: «True»= Nil;say ; # OUTPUT: «False»# access non initialized elementsay ; # OUTPUT: «False»
method ACCEPTS()
Returns True
if $other
and self
contain all the same elements, and no others.
method Bag(Setty: --> Bag)
Returns a Bag containing the elements of the invocant.
my Bag = Set.new(1, 2, 3).Bag;say ; # OUTPUT: «Bag(3 1 2)»
The quantity of the elements in this created bag will be set to one:
say (1,2,3).Bag; # OUTPUT: «1»
method BagHash(Setty: --> BagHash)
Returns a BagHash containing the elements of the invocant.
my BagHash = Set.new(1, 2, 3).BagHash;say ; # OUTPUT: «BagHash(1 2 3)»
multi method Bool(Setty: --> Bool)
Returns True
if the invocant contains at least one element.
my = Set.new(1, 2, 3);say .Bool; # OUTPUT: «True»my = ∩ Set.new(4, 5); # set intersection operatorsay .Bool; # OUTPUT: «False»
method Mix(Setty: --> Mix)
Returns a Mix containing the elements of the invocant.
my Mix = Set.new(1, 2, 3).Mix;say ; # OUTPUT: «Mix(3 1 2)»
The elements of the returned Mix
will have weights equal to 1:
say (1,2,3).Mix; # OUTPUT: «1»
method MixHash(Setty: --> MixHash)
Returns a MixHash containing the elements of the invocant.
my MixHash = Set.new(1, 2, 3).MixHash;say ; # OUTPUT: «MixHash(1 2 3)»