Setty Reference
Collection of distinct objects
What it is #
Position in the hierarchy #
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 new-from-pairs #
method new-from-pairs(*@pairs --> Setty:D)
Returns Setty:D.
Constructs a Setty object from a list of Pair objects given as positional arguments: 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 #
method grab($count = 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 #
method grabpairs($count = 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 Pairs in the aforementioned way in random order.
method pick #
multi method pick($count = 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).
method pickpairs #
multi method pickpairs(Setty:D: --> Pair:D)
multi method pickpairs(Setty:D: $count --> Seq:D)
Returns Pair:D or Seq:D.
Returns a Pair or a Seq of Pairs 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
method roll #
multi method roll($count = 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.
method antipairs #
multi method antipairs(Setty:D: --> Seq:D)
Returns Seq:D.
Returns all elements in the set and True as a Seq of Pairs, where the element itself is the value, i.e. the opposite of method pairs.
method keys #
multi method keys(Setty:D: --> Seq:D)
Returns Seq:D.
Returns a Seq of all elements of the set.
method values #
multi method values(Setty:D: --> Seq:D)
Returns Seq:D.
Returns a Seq containing as many True values as the set has elements.
method kv #
multi method kv(Setty:D: --> Seq:D)
Returns Seq:D.
Returns a Seq of the set's elements and True values interleaved.
method elems #
method elems(Setty:D: --> Int)
Returns Int.
The number of elements of the set.
method total #
method total(Setty:D: --> Int)
Returns Int.
The total of all the values of the QuantHash object. For a Setty object, this is just the number of elements.
method minpairs #
multi method minpairs(Setty:D: --> Seq:D)
Returns Seq:D.
Returns the value of self.pairs (as all Pairs have minimum values). See also Any.minpairs
method maxpairs #
multi method maxpairs(Setty:D: --> Seq:D)
Returns Seq:D.
Returns the value of self.pairs (as all Pairs have maximum values). See also Any.maxpairs
method default #
method default(--> False)
Returns 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.
method ACCEPTS #
method ACCEPTS($other)
Returns True if $other and self contain all the same elements, and no others.
method Bag #
method Bag(Setty:D: --> Bag:D)
Returns Bag:D.
Returns a Bag containing the elements of the invocant. The quantity of the elements in this created bag will be set to one:
method BagHash #
method BagHash(Setty:D: --> BagHash:D)
Returns BagHash:D.
Returns a BagHash containing the elements of the invocant.
method Bool #
multi method Bool(Setty:D: --> Bool:D)
Returns Bool:D.
Returns True if the invocant contains at least one element.
method Mix #
method Mix(Setty:D: --> Mix:D)
Returns Mix:D.
Returns a Mix containing the elements of the invocant. The elements of the returned Mix will have weights equal to 1:
method MixHash #
method MixHash(Setty:D: --> MixHash:D)
Returns MixHash:D.
Returns a MixHash containing the elements of the invocant. 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 · 1 no-output · 1 not-runnable · 9 ok · 1 rakupp-differs
role Setty does QuantHash { }Not executed: the documentation states no expected output for this example.
say Set.new-from-pairs: 'butter' => 0.22, 'salt' => 0, 'sugar' => 0.02; # OUTPUT: «Set(butter sugar)»
Set(butter sugar)
Documentation, Rakudo and Raku++ all agree.
my $numbers = set (4, 2, 3); say $numbers.pickpairs; # OUTPUT: «4 => True» say $numbers.pickpairs(1); # OUTPUT: «(3 => True)» say $numbers.pickpairs(*); # OUTPUT: «(2 => True 4 => True 3 => True)»
4 => True
(3 => True)
(2 => True 4 => True 3 => True)
4 => True
(3 => True)
(3 => True 4 => True 2 => True)
2 => True
(4 => True)
(2 => True 3 => True 4 => True)
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 $s = Set.new(1, 2, 3, 1); say $s.antipairs.sort; # OUTPUT: «(True => 1 True => 2 True => 3)»
(True => 1 True => 2 True => 3)
Documentation, Rakudo and Raku++ all agree.
my $s = Set.new(1, 2, 3); say $s.keys; # OUTPUT: «(3 1 2)»
(3 1 2)
(3 2 1)
(1 2 3)
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 $s = Set.new(1, 2, 3); say $s.values; # OUTPUT: «(True True True)»
(True True True)
Documentation, Rakudo and Raku++ all agree.
my $s = Set.new(1, 2, 3); say $s.kv; # OUTPUT: «(3 True 1 True 2 True)»
(3 True 1 True 2 True)
(3 True 1 True 2 True)
(1 True 2 True 3 True)
Raku++ disagrees with both the documentation and Rakudo — a defect.
my $s1 = SetHash.new(1, 2, 3);
say $s1{2}; # OUTPUT: «True»
$s1{2} = Nil;
say $s1{2}; # OUTPUT: «False»
# access non initialized element
say $s1{4}; # OUTPUT: «False»True
False
False
Documentation, Rakudo and Raku++ all agree.
my Bag $b = Set.new(1, 2, 3).Bag; say $b; # OUTPUT: «Bag(3 1 2)»
Bag(3 1 2)
Bag(1 2 3)
Bag(1 2 3)
Both engines agree; the documentation states something else. Trust the engines.
say (1,2,3).Bag{1}; # OUTPUT: «1»1
Documentation, Rakudo and Raku++ all agree.
my BagHash $b = Set.new(1, 2, 3).BagHash; say $b; # OUTPUT: «BagHash(1 2 3)»
BagHash(1 2 3)
Documentation, Rakudo and Raku++ all agree.
my $s1 = Set.new(1, 2, 3); say $s1.Bool; # OUTPUT: «True»
True
Documentation, Rakudo and Raku++ all agree.
my $s2 = $s1 ∩ Set.new(4, 5); # set intersection operator say $s2.Bool; # OUTPUT: «False»
Neither engine can run this in isolation — the example depends on context from the surrounding text.
my Mix $b = Set.new(1, 2, 3).Mix; say $b; # OUTPUT: «Mix(3 1 2)»
Mix(3 1 2)
Mix(1 2 3)
Mix(1 2 3)
Both engines agree; the documentation states something else. Trust the engines.
say (1,2,3).Mix{3}; # OUTPUT: «1»1
Documentation, Rakudo and Raku++ all agree.
my MixHash $b = Set.new(1, 2, 3).MixHash; say $b; # OUTPUT: «MixHash(1 2 3)»
MixHash(1 2 3)
Documentation, Rakudo and Raku++ all agree.