Documentation for routine unique

Documentation for routine unique, assembled from the following types:

class Any

From Any

(Any) method unique

Defined As:

    method unique(--> TODO)


class List

From List

(List) routine unique

Defined as:

multi sub    unique(*@values:&as:&with --> Seq:D)
multi method unique(List:D:  :&as:&with --> Seq:D)

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:


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})␤» 

class Supply

From Supply

(Supply) 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 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.