In Raku, I can create a set of lists:
> my $set = SetHash.new((1, 2), (3, 4, 5))
SetHash((1 2) (3 4 5))
> $set.keys.map(&WHAT)
((List) (List))
But I can't seem to check for the existence of a list key:
> $set{(1,2)}
(False False)
...because the list in the subscript is interpreted as a slice, not as a single key.
Is there any way to look up such a key?
The first element of a list is at index number zero: Variables in Raku whose names bear the @ sigil are expected to contain some sort of list-like object. Of course, other variables may also contain these objects, but @ -sigiled variables always do, and are expected to act the part.
These are called sequences, which are of type Seq. As it so happens, loops return Seq s. So, it is fine to have infinite lists in Raku, just so long as you never ask them for all their elements.
For most uses, Array s consist of a number of slots each containing a Scalar of the correct type. Every such Scalar, in turn, contains a value of that type. Raku will automatically type-check values and create Scalars to contain them when Arrays are initialized, assigned to, or constructed.
Variables in Raku whose names bear the @ sigil are expected to contain some sort of list-like object. Of course, other variables may also contain these objects, but @ -sigiled variables always do, and are expected to act the part. By default, when you assign a List to an @ -sigiled variable, you create an Array.
Sets work on ValueTypes. Even though a List may seem like a ValueType, unfortunately it is not (because although the number of elements in a List is fixed, it may contain mutable elements, and is therefore not always a constant).
That's why I implemented the Tuple module a few years ago already. This allows you to:
use Tuple;
my $set := SetHash.new: tuple(1,2), tuple(1,2,3);
say $set{tuple(1,2)}; # True
Granted, a bit verbose. You can shorten the verbosity by re-defining the tuple
sub:
use Tuple;
my &t := &tuple;
my $set := SetHash.new: t(1,2), t(1,2,3);
say $set{t(1,2)}; # True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With