Looking up list keys in a Raku set

Viewed 87

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?

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
Related