Skip to content

Commit eb5f721

Browse files
committed
Document 'unique' and 'squish', mostly copying the descriptions from S32.
1 parent b76872c commit eb5f721

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

lib/Type/List.pod

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,54 @@ Examples:
314314
say (3, -4, 7, -1, 2, 0).sort: *.abs; # 0 -1 2 3 -4 7
315315
say (3, -4, 7, -1, 2, 0).sort: { $^b leg $^a }; # 7 3 2 0 -4 -1
316316
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+
317365
=head2 routine reduce
318366
319367
multi sub reduce(&with, *@elems)

0 commit comments

Comments
 (0)