use v6;

=begin pod

=TITLE P19 - Rotate a list C<n> places to the left.

=AUTHOR Ryan Connelly

=head1 Example

    > say rotate(<a b c d e f g>.list, 3).perl;
    Array.new("d", "e", "f", "g", "a", "b", "c")

=end pod

sub rotate(@list is copy, $places is copy)
{
    $places < 0 ?? ( $places = @list.elems + $places) !! ();

    for ^$places {
        @list.push: @list.shift;
    }

    @list
}

say rotate(<a b c d e f g>.list, 3).perl;
say rotate(<a b c d e f g>.list, -3).perl;

# vim: expandtab shiftwidth=4 ft=perl6

use v6;

=begin pod

=TITLE P19 - Rotate a list C<n> places to the left.

=AUTHOR Ryan Connelly

=head1 Example

    > say rotate(<a b c d e f g>.list, 3).perl;
    Array.new("d", "e", "f", "g", "a", "b", "c")

=end pod

sub rotate(@list is copy, $places is copy)
{
    $places = @list.elems + $places if $places < 0;

    for ^$places {
        @list.push: @list.shift;
    }

    @list
}

say rotate(<a b c d e f g>.list, 3).perl;
say rotate(<a b c d e f g>.list, -3).perl;

# vim: expandtab shiftwidth=4 ft=perl6

Simplify calculation of places to rotate
Simplify calculation of places to rotate

The previous calculation used a ternary (?? !!) expression to decide whether
or not to calculat the value of the `places` variable.  This had the "or"
part of the ternary conditional operator use a null sink and hence this line
could simply be replaced by a postfix `if`.

This removes the "Useless use of () in sink context" warning we were
getting.
