Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeated and unique in hash-like things

Tags:

raku

The repeated method takes a function as an argument for normalizing the elements before finding out which ones are repeated. However, I can't seen to make it work with values. For instance:

%(:a(3),:b(3),:c(2)).repeated( as=> *.values ).say

Returns an empty list, while I was expecting the pairs :a(3) and :b(3), same as

%(:a(3),:b(3),:c(2)).repeated( as=> .values ).say

In this case, for instance, it seems to work as expected:

(3+3i, 3+2i, 2+1i).unique(as => *.re).say  # OUTPUT: «(3+3i 2+1i)␤»

Any idea of what I'm missing here?

like image 324
jjmerelo Avatar asked Feb 17 '19 17:02

jjmerelo


1 Answers

.values is a method for returning all of the values of a container.

Since it is a List method, if you call it on a singular value it pretends it is a List containing only that value.

say 5.values.perl;
# (5,)

The as named parameter of .repeated gets called on all of the singular values.

%(:a(3),:b(3),:c(2)).repeated( as=> *.perl.say );
# :a(3)
# :b(3)
# :c(2)

So by giving it the *.values lambda, it is effectively not doing anything useful.


The method you were looking for is .value. Which is a method on a Pair.

%(:a(3),:b(3),:c(2)).repeated( as=> *.value ).say
# (a => 3)
like image 135
Brad Gilbert Avatar answered Nov 04 '22 02:11

Brad Gilbert