Skip to content

Commit

Permalink
squish and repeated were not really in List
Browse files Browse the repository at this point in the history
Moved to independent routines, with the sub only definition. Refs #1518
  • Loading branch information
JJ committed May 24, 2019
1 parent 311e8d2 commit 03f0e18
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 71 deletions.
74 changes: 7 additions & 67 deletions doc/Type/List.pod6
Expand Up @@ -827,67 +827,6 @@ say <01 11 111 2 20 02>.sort( { .Int, .comb.sum, .Str } );
# OUTPUT: «(01 02 2 11 20 111)␤»
=end code
=head2 routine repeated
Defined as:
multi sub repeated(+values, |c)
multi method repeated()
multi method repeated( :&as!, :&with! )
multi method repeated( :&as! )
multi method repeated( :&with! )
This is directly inherited from L<C<Any>>; and it returns a sequence of
B<repeated> values from the invocant/argument list. It takes the same parameters
as L<C<unique>>, but instead of passing through any elements when they're first
seen, they're only passed through as soon as they're seen for the second time
(or more).
Examples:
say <a a b b b c c>.repeated; # OUTPUT: «(a b b c)␤»
say <a b b c c b a>.repeated; # OUTPUT: «(b c b a)␤»
say <a A B b c b C>.repeated(:as(&lc)); # OUTPUT: «(A b b C)␤»
my @list = %(a => 42), %(b => 13), %(a => 42);
say @list.repeated(:with(&[eqv])) # OUTPUT: «({a => 42})␤»
As in the case of L<C<unique>|/type/Any#method_unique> the associative argument
C<:as> takes a Callable that normalizes the element before comparison, and
C<:with> takes a the equality comparison function that is going to be used.
=head2 routine squish
Defined as:
multi method squish( :&as!, :&with = &[===] )
multi method squish( :&with = &[===] )
sub squish( +values, |c)
Returns a sequence of values from the invocant/argument list where runs of one
or more values are replaced with only the first instance. Like L<C<unique>>,
C<squish> uses the semantics of the L<===> operator to decide whether two
objects are the same. Unlike L<C<unique>>, this function only removes adjacent
duplicates; identical values further apart are still kept. The order of the
original list is preserved even as duplicates are removed.
Examples:
say <a a b b b c c>.squish; # OUTPUT: «(a b c)␤»
say <a b b c c b a>.squish; # OUTPUT: «(a b c b a)␤»
The optional C<:as> parameter, just like with L<C<unique>>, allows values to be
temporarily transformed before comparison.
The optional C<:with> parameter is used to set an appropriate comparison
operator:
say [42, "42"].squish; # OUTPUT: «(42 42)␤»
# Note that the second item in the result is still Str
say [42, "42"].squish(with => &infix:<eq>); # OUTPUT: «(42)␤»
# The resulting item is Int
=head2 routine reduce
Defined as:
Expand Down Expand Up @@ -1091,13 +1030,14 @@ Returns all possible permutations of a list as a L<Seq|/type/Seq> of lists:
# (c a b)
# (c b a)
C<permutations> treats all elements as unique, thus C<(1, 1, 2).permutations> returns
a list of 6 elements, even though there are only three distinct permutations, due to first two
elements being the same.
C<permutations> treats all elements as unique, thus C<(1, 1, 2).permutations>
returns a list of 6 elements, even though there are only three distinct
permutations, due to first two elements being the same.
The subroutine form behaves the same as the method form, computing permutations from its
first argument C<$from>. If C<$from> is not an L<Iterable|/type/Iterable>, coerces C<$from> to an C<Int> and
picks from a L<Range|/type/Range> constructed with C<0..^$from>:
The subroutine form behaves the same as the method form, computing permutations
from its first argument C<$from>. If C<$from> is not an
L<Iterable|/type/Iterable>, coerces C<$from> to an C<Int> and picks from a
L<Range|/type/Range> constructed with C<0..^$from>:
.say for permutations 3;
# OUTPUT:
Expand Down
58 changes: 54 additions & 4 deletions doc/Type/independent-routines.pod6
Expand Up @@ -943,10 +943,6 @@ on that list or C<Iterable>:
Defined as:
multi method unique()
multi method unique( :&as!, :&with! )
multi method unique( :&as! )
multi method unique( :&with! )
multi sub unique(+values, |c)
Returns a sequence of B<unique> values from the invocant/argument list, such
Expand Down Expand Up @@ -987,6 +983,60 @@ items in the list, this makes C<unique> follow a path with much higher
algorithmic complexity. You should try to use the C<:as> argument instead,
whenever possible.
=head2 routine repeated
Defined as:
multi sub repeated(+values, |c)
This returns a sequence of B<repeated> values from the invocant/argument list.
It takes the same parameters as L<C<unique>>, but instead of passing through any
elements when they're first seen, they're only passed through as soon as they're
seen for the second time (or more).
Examples:
say <a a b b b c c>.repeated; # OUTPUT: «(a b b c)␤»
say <a b b c c b a>.repeated; # OUTPUT: «(b c b a)␤»
say <a A B b c b C>.repeated(:as(&lc)); # OUTPUT: «(A b b C)␤»
my @list = %(a => 42), %(b => 13), %(a => 42);
say @list.repeated(:with(&[eqv])) # OUTPUT: «({a => 42})␤»
As in the case of L<C<unique>|/type/Any#method_unique> the associative argument
C<:as> takes a Callable that normalizes the element before comparison, and
C<:with> takes a the equality comparison function that is going to be used.
=head2 routine squish
Defined as:
sub squish( +values, |c)
Returns a sequence of values from the invocant/argument list where runs of one
or more values are replaced with only the first instance. Like L<C<unique>>,
C<squish> uses the semantics of the L<===> operator to decide whether two
objects are the same. Unlike L<C<unique>>, this function only removes adjacent
duplicates; identical values further apart are still kept. The order of the
original list is preserved even as duplicates are removed.
Examples:
say <a a b b b c c>.squish; # OUTPUT: «(a b c)␤»
say <a b b c c b a>.squish; # OUTPUT: «(a b c b a)␤»
The optional C<:as> parameter, just like with L<C<unique>>, allows values to be
temporarily transformed before comparison.
The optional C<:with> parameter is used to set an appropriate comparison
operator:
say [42, "42"].squish; # OUTPUT: «(42 42)␤»
# Note that the second item in the result is still Str
say [42, "42"].squish(with => &infix:<eq>); # OUTPUT: «(42)␤»
# The resulting item is Int
=end pod

# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 comments on commit 03f0e18

Please sign in to comment.