Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
List.rotor
  • Loading branch information
moritz committed Apr 24, 2015
1 parent f3218dc commit 003d721
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
1 change: 0 additions & 1 deletion WANTED
Expand Up @@ -22,4 +22,3 @@ API docs:

Builtins:
* Str.substr-eq
* List.rotor
33 changes: 33 additions & 0 deletions lib/Type/List.pod
Expand Up @@ -537,6 +537,39 @@ prints
3 1 2
3 2 1
=head2 method rotor
method rotor(*@cylce, Bool() :$partial)
Returns a list of list, which each sublist is made of elements of the
invocant.
In the simplest case, C<@cycle> contains just one integer, in which case the
invocant list is split into sublists with as many elements as the integer
specifies. If C<:$partial> is True, the final chunk is included even if it
doesn't satisfy the length requirement:
say ('a'..'h').rotor(3).join('|'); # a b c|d e f
say ('a'..'h').rotor(3, :partial).join('|'); # a b c|d e f|g h
If the element of C<@cycle> is a L</type/Pair|Pair> instead, the key of the
pair specifies the length of the return sublist, and the value the gap betwee
sublists; negative gaps produce overlap:
say ('a'..'h').rotor(2 => 1).join('|'); # a b|d e|g h
say ('a'..'h').rotor(3 => -1).join('|'); # a b c|c d e|e f g
If C<@cycle> contains more than element, C<rotor> cycles through it to find
the number of elements for each sublist:
say ('a'..'h').rotor(2, 3).join('|'); # a b|c d e|f g
say ('a'..'h').rotor(1 => 1, 3).join('|'); # a|c d e|f
Combining multiple cycles and C<:partial> also works:
say ('a'..'h').rotor(1 => 1, 3 => -1, :partial).join('|');
# a|c d e|e|g h
=head2 routine zip
sub zip(List:D:, List:D:, ...) returns List:D
Expand Down

0 comments on commit 003d721

Please sign in to comment.