@@ -314,6 +314,54 @@ Examples:
314
314
say (3, -4, 7, -1, 2, 0).sort: *.abs; # 0 -1 2 3 -4 7
315
315
say (3, -4, 7, -1, 2, 0).sort: { $^b leg $^a }; # 7 3 2 0 -4 -1
316
316
317
+ = head2 routine unique
318
+
319
+ multi sub unique(*@values, :&as) returns List:D
320
+ multi method unique(List:D:, :&as) returns List:D
321
+
322
+ Returns a list of unique values from the invocant/argument list, such
323
+ that only the first occurrence of each duplicated value remains in the
324
+ result list. C < unique > uses C << &infix:<===> >> semantics to compare whether
325
+ two objects are the same. The order of the original list is preserved even as
326
+ duplicates are removed.
327
+
328
+ Examples:
329
+
330
+ say <a a b b b c c>.unique # a b c
331
+ say <a b b c c b a>.unique # a b c
332
+
333
+ (Use L < C < squish > > instead if you know the input is sorted such that identical
334
+ objects are adjacent.)
335
+
336
+ The optional C < :as > parameter allows you to normalize/canonicalize the elements
337
+ before unique-ing. The values are transformed for the purposes of comparison, but
338
+ it's still the original values that make it to the result list:
339
+
340
+ Example:
341
+
342
+ say <a A B b c b C>.unique(:as(&lc)) # a B c
343
+
344
+ = head2 routine squish
345
+
346
+ multi sub squish(*@values, :&as) returns List:D
347
+ multi method squish(List:D:, :&as) returns List:D
348
+
349
+ Returns a list of values from the invocant/argument list where runs
350
+ of more than one value are replaced with only the first instance.
351
+ Like L < C < unique > > , C < squish > uses C << &infix:<===> >> semantics to compare
352
+ whether two objects are the same. Unlike L < C < unique > > , this function only
353
+ removes adjacent duplicates; identical values further apart are still
354
+ kept. The order of the original list is preserved even as duplicates
355
+ are removed.
356
+
357
+ Examples:
358
+
359
+ say <a a b b b c c>.squish # a b c
360
+ say <a b b c c b a>.squish # a b c b a
361
+
362
+ The optional C < :as > parameter, just like with L < C < unique > > , allows values to be
363
+ temporarily transformed before comparison.
364
+
317
365
= head2 routine reduce
318
366
319
367
multi sub reduce(&with, *@elems)
0 commit comments