Skip to content

Commit afb84de

Browse files
committed
[List] sort, rotate
1 parent 7233ae8 commit afb84de

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lib/List.pod

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,33 @@ stop at the point where the infinity is detected.
189189
190190
Returns a list with the elements in reverse order.
191191
192+
=head3 rotate
193+
194+
multi sub rotate(@list, Int:D $n = 1) returns List:D
195+
multi method rotate(List:D: Int:D $n = 1) returns List:D
196+
197+
Returns the list rotated by C<$n> elements.
198+
199+
<a b c d e>.rotate(2); # <c d e a b>
200+
<a b c d e>.rotate(-1); # <e a b c d>
201+
202+
=head3 sort
203+
204+
multi sub sort(*@elems) returns List:D
205+
multi sub sort(&by, *@elems) returns List:D
206+
multi method sort(List:D:) returns List:D
207+
multi method sort(List:D:, &by) returns List:D
208+
209+
Sorts the list, smallest element first. By default C<< infix:<cmp> >>
210+
is used for comparing list elements.
211+
212+
If C<&by> is provided, and it accepts two arguments,
213+
it is invoked for pairs of list elements, and should return
214+
C<Order::Increase>, C<Order::Same> or C<Order::Decrease>.
215+
216+
If C<&by> accepts only one argument, the list elements are sorted
217+
according to C<< by($a) cmp by($b) >>. The return values of C<&by> are
218+
cached, so that C<&by> is only called once per list element.
219+
220+
192221
=end pod

0 commit comments

Comments
 (0)