Skip to content

Commit

Permalink
Add a few more List examples
Browse files Browse the repository at this point in the history
  • Loading branch information
softmoth committed Aug 25, 2012
1 parent 2957324 commit dad8272
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions lib/List.pod
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ Example:
> ('hello', 1, 22/7, 42, 'world').classify: { .Str.chars }
("5" => ["hello", "world"], "1" => [1], "8" => [22/7], "2" => [42]).hash
Creates the hash C<< { even => [6, 2], odd => [1, 7, 3] } >> in C<%h>.
=head2 Bool
multi method Bool(List:D:) returns Bool:D
Expand Down Expand Up @@ -307,6 +305,11 @@ generated that way.
Note that C<reduce> is an implicit loop, and thus responds to C<next>, C<last>
and C<redo> statements.
Example:
> (1, 2, 3).reduce: * - *;
-4
=head2 splice
multi sub splice(@list, $start, $elems?, *@replacement) returns List:D
Expand All @@ -316,13 +319,33 @@ Deletes C<$elems> elements starting from index C<$start> from the list,
returns them and replaces them by C<@replacement>. If C<$elems> is omitted,
all the elements starting from index C<$start> are deleted.
Example:
> my @foo = <a b c d e f g>;
a b c d e f g
> @foo.splice(2, 3, <M N O P>);
c d e
> @foo
a b M N O P f g
=head2 pop
multi sub pop(List:D )
multi method pop(List:D:)
Removes and returns the last item from the list, fails for an empty list.
Example:
> my @foo = <a b>;
a b
> @foo.pop;
b
> pop @foo
a
> pop @foo
Element popped from empty list
=head2 push
multi sub push(List:D, *@values) returns List:D
Expand All @@ -331,13 +354,29 @@ Removes and returns the last item from the list, fails for an empty list.
Adds the C<@values> to the end of the list, and returns the modified list.
Fails for infinite lists.
Example:
> my @foo = <a b c>; @foo.push: 1, 3 ... 11;
a b c 1 3 5 7 9 11
=head2 shift
multi sub shift(List:D )
multi method shift(List:D:)
Removes and returns the first item from the list. Fails for an empty list.
Example:
> my @foo = <a b>;
a b
> @foo.shift;
a
> @foo.shift;
b
> @foo.shift;
Element shifted from empty list
=head2 unshift
multi sub unshift(List:D, *@values) returns List:D
Expand All @@ -346,4 +385,9 @@ Removes and returns the first item from the list. Fails for an empty list.
Adds the C<@values> to the start of the list, and returns the modified list.
Fails if C<@values> is infinite.
Example:
> my @foo = <a b c>; @foo.unshift: 1, 3 ... 11;
1 3 5 7 9 11 a b c
=end pod

0 comments on commit dad8272

Please sign in to comment.