In Any§

See primary documentation in context for method unique

multi method unique()
multi method unique:&as!:&with! )
multi method unique:&as! )
multi method unique:&with! )

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""2).uniqueas => Intwith => &[==] ).say# OUTPUT: «(1 2)␤»

Please see unique for additional examples that use its sub form.

In Independent routines§

See primary documentation in context for routine unique

multi 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.


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:


say <a A B b c b C>.unique(:as(&lc))      # 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.


my @list = %(=> 42), %(=> 13), %(=> 42);
say @list.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.

In Supply§

See primary documentation in context for method unique

method unique(Supply:D: :$as:$with:$expires --> Supply:D)

Creates a supply that only provides unique values, as defined by the optional :as and :with parameters (same as with 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.