routine unique
1 | class Supply |
1.1 | (Supply) method unique |
2 | class Any |
2.1 | (Any) method unique |
3 | language documentation Independent routines |
3.1 | (Independent routines) routine unique |
Documentation for routine unique
assembled from the following types:
class Supply
From Supply
(Supply) method unique
method unique(Supply: :, :, : --> Supply)
Creates a supply that only provides unique values, as defined by the optional :as
and :with
parameters (same as with List.unique). The optional :expires
parameter how long to wait (in seconds) before "resetting" and not considering a value to have been seen, even if it's the same as an old value.
class Any
From Any
(Any) method unique
Defined as:
multi method unique()multi method unique( :!, :! )multi method unique( :! )multi method unique( :! )
Creates a sequence of unique elements either of the object or of values
in the case it's called as a sub
.
<1 2 2 3 3 3>.unique.say; # OUTPUT: «(1 2 3)»say unique <1 2 2 3 3 3>; # OUTPUT: «(1 2 3)»
The :as
and :with
parameters receive functions that are used for transforming the item before checking equality, and for checking equality, since by default the ===
operator is used:
("1", 1, "1 ", 2).unique( as => Int, with => &[==] ).say; #OUTPUT: «(1 2)»
Please see unique
for additional examples that use its sub form.
language documentation Independent routines
From Independent routines
(Independent routines) routine unique
Defined as:
multi sub unique(+values, |c)
Returns a sequence of unique values from the invocant/argument list, such that only the first occurrence of each duplicated value remains in the result list. unique
uses the semantics of the === operator to decide whether two objects are the same, unless the optional :with
parameter is specified with another comparator. The order of the original list is preserved even as duplicates are removed.
Examples:
say <a a b b b c c>.unique; # OUTPUT: «(a b c)»say <a b b c c b a>.unique; # OUTPUT: «(a b c)»
(Use squish instead if you know the input is sorted such that identical objects are adjacent.)
The optional :as
parameter allows you to normalize/canonicalize the elements before unique-ing. The values are transformed for the purposes of comparison, but it's still the original values that make it to the result list; however, only the first occurrence will show up in that list:
Example:
say <a A B b c b C>.unique(:as()) # OUTPUT: «(a B c)»
One can also specify the comparator with the optional :with
parameter. For instance if one wants a list of unique hashes, one could use the eqv
comparator.
Example:
my = %(a => 42), %(b => 13), %(a => 42);say .unique(:with(&[eqv])) # OUTPUT: «({a => 42} {b => 13})»
Note: since :with
Callable has to be tried with all the items in the list, this makes unique
follow a path with much higher algorithmic complexity. You should try to use the :as
argument instead, whenever possible.