Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does element membership work in Perl 6?

Tags:

set

raku

Consider this example

my @fib =  (1,1, * + * … * > 200).rotor(2 => -1); 
say @fib[0] ∈  @fib; # prints True

The first statement creates a Sequence of 2-element subsequences via the use of the rotor function. @fib will contain (1,1), (1,2) and so on. Quite obviously, the first element of a sequence is part of a sequence. Or is it?

my @fib =  (1,1, * + * … * > 200).rotor(2 => -1); 
say @fib[0], @fib[0].^name; # OUTPUT: «(1 1)List␤»

So the first element contains a list whose value is (1 1). OK, let's see

my $maybe-element = (1,1); 
say $maybe-element, $maybe-element.^name; # OUTPUT: «(1 1)List␤»
say $maybe-element ∈  @fib;               # OUTPUT: «False␤»

Wait, what? Let's see...

my $maybe-element = @fib[0]; 
say $maybe-element ∈ @fib; # OUTPUT: «True␤»

Hum. So it's not the container. But

say (1,1).List === (1,1).List; # OUTPUT: «False␤»

And

say (1,1).List == (1,1).List; # OUTPUT: «True␤»

So I guess is using object identity, and not equality. That being the case, how can we check, in sets or sequences of lists, if an independently generated list is included using this operator? Should we use another different strategy?

Maybe a subquestion is why the same literals generate completely different objects, but there's probably a good, and very likely security-related, answer for that.

like image 616
jjmerelo Avatar asked May 25 '18 08:05

jjmerelo


1 Answers

So I guess ∈ is using object identity, and not equality.

That is correct.

That being the case, how can we check, in sets or sequences of lists, if an independently generated list is included using this operator?

You can use .grep or .first and the equality operator of your choice (presumably you want eqv here), or you can try to find a list-like value type. Off the top of my head, I don't know if one is built into Perl 6.

like image 112
moritz Avatar answered Sep 21 '22 16:09

moritz