@@ -556,19 +556,20 @@ Examples:
556
556
557
557
Defined as:
558
558
559
- multi sub unique(*@values, :&as) returns Seq:D
560
- multi method unique(List:D:, :&as) returns Seq:D
559
+ multi sub unique(*@values, :&as, :&with ) returns Seq:D
560
+ multi method unique(List:D:, :&as, :&with ) returns Seq:D
561
561
562
562
Usage:
563
563
564
- unique LIST, :COMPARE_AS
565
- LIST.unique(:COMPARE_AS)
564
+ unique LIST, :COMPARE_AS, :COMPARE_WITH
565
+ LIST.unique(:COMPARE_AS, :COMPARE_WITH )
566
566
567
567
Returns a sequence of unique values from the invocant/argument list, such
568
568
that only the first occurrence of each duplicated value remains in the
569
- result list. C < unique > uses the semantics of the L < === > operator to decide whether
570
- two objects are the same. The order of the original list is preserved even as
571
- duplicates are removed.
569
+ result list. C < unique > uses the semantics of the L < === > operator to decide
570
+ whether two objects are the same, unless the optional C < :with > parameter is
571
+ specified with another comparator. The order of the original list is preserved
572
+ even as duplicates are removed.
572
573
573
574
Examples:
574
575
@@ -579,13 +580,22 @@ Examples:
579
580
objects are adjacent.)
580
581
581
582
The optional C < :as > parameter allows you to normalize/canonicalize the elements
582
- before unique-ing. The values are transformed for the purposes of comparison, but
583
- it's still the original values that make it to the result list:
583
+ before unique-ing. The values are transformed for the purposes of comparison,
584
+ but it's still the original values that make it to the result list:
584
585
585
586
Example:
586
587
587
588
say <a A B b c b C>.unique(:as(&lc)) # a B c
588
589
590
+ One can also specify the comparator with the optional C < :with > parameter. For
591
+ instance if one wants a list of unique hashes, one could use the C < eqv >
592
+ comparator.
593
+
594
+ Example:
595
+
596
+ my @list = {a => 42}, {b => 13}, {a => 42};
597
+ say @list.unique(:with(&[eqv])) # {a=>42} {b=>13}
598
+
589
599
= head2 routine squish
590
600
591
601
Defined as:
0 commit comments