The docs explain how to normalize list items before calling .unique
:
Optional parameter: as allows you to normalize / canonize elements before unique. Values are converted for comparison purposes, but these are still the original values that make this a list of results.
and the following example is given:
say <a A B b c b C>.unique(:as(&lc)) # OUTPUT: «(a B c)»
What if I want to make the list of rational numbers unique by comparing only their integer part? How can I call the method Int
in parentheses after :as
?
my @a = <1.1 1.7 4.2 3.1 4.7 3.2>;
say @a.unique(:as(?????)) # OUTPUT: «(1.1 4.2 3.1)»
UPD:
Based on @ Håkon's answer, I found the following solution:
> say @a.unique(:as({$^a.Int}));
(1.1 4.2 3.1)
or
> say @a.unique(as => {$^a.Int});
(1.1 4.2 3.1)
Can this be done without $^a
?
UPD2:
Yes, there it is!
> say @a.unique(as => *.Int);
(1.1 4.2 3.1)
or
> say (3, -4, 7, -1, 1, 4, 2, -2, 0).unique(as => *²)
> (3 -4 7 -1 2 0)
or
> say @a.unique: :as(*.Int);
(1.1 4.2 3.1)
source
share