Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking up list keys in a Raku set

Tags:

raku

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?

like image 651
Sean Avatar asked Dec 09 '21 20:12

Sean


People also ask

What is the first element of a list in raku?

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.

Can We have infinite lists in raku?

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.

What is an array in raku?

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.

What are @-sigiled variables in raku?

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.


Video Answer


1 Answers

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
like image 56
Elizabeth Mattijsen Avatar answered Oct 01 '22 20:10

Elizabeth Mattijsen