Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

379 lines (244 sloc) 10.314 kb
=begin pod
=TITLE class List
my class List is Iterable does Positional { .. }
C<List> stores items sequentially and potentially lazily.
Indexes into lists and arrays start at 0 by default.
You can assign to list elements if they are containers. Use
Arrays to have every value of the list stored in a container.
=head1 Items, Flattening and Sigils
In Perl 6, assigning a C<List> to a scalar variable does not lose
information. The difference is that iteration generally treats a
list (or any other list-like object, like a C<Parcel> or an C<Array>)
inside a scalar as a single element.
my @a = 1, 2, 3;
for @a { } # three iterations
my $s = @a;
for $s { } # one iteration
for @a.item { } # one iteration
for $s.list { } # three iterations
Lists generally interpolate (flatten) unless they are accessed via
an item (scalar) container.
my @a = 1, 2, 3;
my @flat = @a, @a; # six elements
my @nested = @a.item, @a.item; # two elements
C<.item> can often be written as C<$( ... )>, and on an array variable
even as C<$@a>.
=head1 Methods
=head2 elems
multi sub elems($list) returns Int:D
multi method elems(List:D:) returns Int:D
Returns the number of elements in the list.
=head2 end
multi sub end($list) returns Int:D
multi method end(List:D:) returns Int:D
Returns the index of the last element.
=head2 keys
multi sub keys($list) returns List:D
multi method keys(List:D:) returns List:D
Returns a list of indexes into the list (i.e., 0..(@list.elems-1)).
=head2 values
multi sub values($list) returns List:D
multi method values(List:D:) returns List:D
Returns a copy of the list.
=head2 kv
multi sub kv($list) returns List:D
multi method kv(List:D:) returns List:D
Returns an interleaved list of indexes and values. For example
<a b c>.kv
0, 'a', 1, 'b', 2, 'c'
=head2 pairs
multi sub pairs($list) returns List:D
multi method pairs(List:D:) returns List:D
Returns a list of pairs, with the indexes as keys and the list values as
<a b c>.pairs # 0 => 'a', 1 => 'b', 2 => 'c'
=head2 join
multi sub join($separator, *@list) returns Str:D
multi method join(List:D: $separator) returns Str:D
Treats the elements of the list as strings, interleaves them with
C<$separator> and concatenates everything into a single string.
join ', ', <a b c>; # 'a, b, c'
=head2 map
multi sub map(&code, *@elems) returns List:D
multi method map(List:D: &code) returns List:D
Invokes C<&code> for each element and gathers the return values in another
list and returns it. This happens lazily, ie C<&code> is only invoked when
the return values are accessed.
> ('hello', 1, 22/7, 42, 'world').map: { .WHAT.perl }
Str Int Rat Int Str
> map *.Str.chars, 'hello', 1, 22/7, 42, 'world'
5 1 8 2 5
=head2 grep
multi sub grep(Mu $matcher, *@elems) returns List:D
multi method grep(List:D: Mu $matcher) returns List:D
Returns a lazy list of elements against which C<$matcher> smart-matches.
The elements are returned in the order in which they appear in the original
> ('hello', 1, 22/7, 42, 'world').grep: Int
1 42
> grep { .Str.chars > 3 }, 'hello', 1, 22/7, 42, 'world'
hello 3.142857 world
=head2 first
multi sub first(Mu $matcher, *@elems)
multi method first(List:D: Mu $matcher)
Returns the first item of the list which smart-matches against C<$matcher>,
fails when no values match.
say (1, 22/7, 42).first: * > 5; # 42
say $f = ('hello', 1, 22/7, 42, 'world').first: Complex;
say $f.perl; # => => "No values matched"))
=head2 classify
multi sub classify(&mapper, *@values) returns Hash:D
multi method classify(List:D: &mapper) returns Hash:D
Transforms a list of values into a hash
representing the classification of those values according to a mapper;
each hash key represents the classification for one or more of the
incoming list values, and the corresponding hash value contains
an array of those list values classified by the mapper into the category
of the associated key.
say classify { $_ %% 2 ?? 'even' !! 'odd' }, (1, 7, 6, 3, 2);
# ("odd" => [1, 7, 3], "even" => [6, 2]).hash;;
say ('hello', 1, 22/7, 42, 'world').classify: { .Str.chars }
# ("5" => ["hello", "world"], "1" => [1], "8" => [22/7], "2" => [42]).hash
=head2 Bool
multi method Bool(List:D:) returns Bool:D
Returns C<True> if the list has at least one element, and C<False>
for the empty list.
=head2 Str
multi method Str(List:D:) returns Str:D
Stringifies the elements of the list and joins them with spaces
(same as C<.join(' ')>).
=head2 Int
multi method Int(List:D:) return Int:D
Returns the number of elements in the list (same as C<.elems>).
=head2 pick
multi sub pick($count, *@list) returns List:D
multi method pick(List:D: $count = 1)
Returns C<$count> elements chosen at random and without repetition
from the invocant. If C<*> is passed as C<$count>, or C<$count> is
greater than or equal to the size of the list, then all elements
from the invocant list are returned in a random sequence.
say <a b c d e>.pick; # b
say <a b c d e>.pick: 3; # c a e
say <a b c d e>.pick: *; # e d a b c
=head2 roll
multi sub roll($count, *@list) returns List:D
multi method roll(List:D: $count = 1)
Returns a lazy list of C<$count> elements, each randomly selected from the
list. Each random choice is made indepently, like a separate die roll
where each die face is a list element.
If C<*> is passed to C<$count>, returns a lazy, infinite list of randomly chosen
elements from the original list.
say <a b c d e>.roll; # b
say <a b c d e>.roll: 3; # c c e
say roll 8, <a b c d e>; # b a e d a e b c
my $random_digits := (^10).roll(*);
say $random_digits[^15]; # 3 8 7 6 0 1 3 2 0 8 8 5 8 0 5
=head2 eager
multi method eager(List:D:) returns List:D
Evaluates all elements in the list eagerly, and returns the invocant.
If a List signals that it is "known infinite", eager evaluation may
stop at the point where the infinity is detected.
=head2 reverse
multi sub reverse(*@list ) returns List:D
multi method reverse(List:D:) returns List:D
Returns a list with the same elements in reverse order.
Note that C<reverse> always refers to reversing elements of a list;
to reverse the characters in a string, use L<flip>.
say <hello world!>.reverse # world! hello
say reverse ^10 # 9 8 7 6 5 4 3 2 1 0
=head2 rotate
multi sub rotate(@list, Int:D $n = 1) returns List:D
multi method rotate(List:D: Int:D $n = 1) returns List:D
Returns the list rotated by C<$n> elements.
<a b c d e>.rotate(2); # <c d e a b>
<a b c d e>.rotate(-1); # <e a b c d>
=head2 sort
multi sub sort(*@elems) returns List:D
multi sub sort(&by, *@elems) returns List:D
multi method sort(List:D:) returns List:D
multi method sort(List:D:, &by) returns List:D
Sorts the list, smallest element first. By default C<< infix:<cmp> >>
is used for comparing list elements.
If C<&by> is provided, and it accepts two arguments,
it is invoked for pairs of list elements, and should return
C<Order::Increase>, C<Order::Same> or C<Order::Decrease>.
If C<&by> accepts only one argument, the list elements are sorted
according to C<< by($a) cmp by($b) >>. The return values of C<&by> are
cached, so that C<&by> is only called once per list element.
say (3, -4, 7, -1, 2, 0).sort; # -4 -1 0 2 3 7
say (3, -4, 7, -1, 2, 0).sort: *.abs; # 0 -1 2 3 -4 7
say (3, -4, 7, -1, 2, 0).sort: { $^b leg $^a }; # 7 3 2 0 -4 -1
=head2 reduce
multi sub reduce(&with, *@elems)
multi method reduce(List:D: &with)
Applies C<&with> to the first and the second value of the list, then to the
result of that calculation and the third value and so on. Returns a single item
generated that way.
Note that C<reduce> is an implicit loop, and thus responds to C<next>, C<last>
and C<redo> statements.
say (1, 2, 3).reduce: * - *; # -4
=head2 splice
multi sub splice(@list, $start, $elems?, *@replacement) returns List:D
multi method splice(List:D: $start, $elems?, *@replacement) returns List:D
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.
my @foo = <a b c d e f g>;
say @foo.splice(2, 3, <M N O P>); # c d e
say @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.
> my @foo = <a b>;
a b
> @foo.pop;
> pop @foo
> pop @foo
Element popped from empty list
=head2 push
multi sub push(List:D, *@values) returns List:D
multi method push(List:D: *@values) returns List:D
Adds the C<@values> to the end of the list, and returns the modified list.
Fails for infinite lists.
my @foo = <a b c>;
@foo.push: 1, 3 ... 11;
say @foo; # 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.
my @foo = <a b>;
say @foo.shift; # a
say @foo.shift; # b
say @foo.shift; # Element shifted from empty list
=head2 unshift
multi sub unshift(List:D, *@values) returns List:D
multi method unshift(List:D: *@values) returns List:D
Adds the C<@values> to the start of the list, and returns the modified list.
Fails if C<@values> is infinite.
my @foo = <a b c>;
@foo.unshift: 1, 3 ... 11;
say @foo; # 1 3 5 7 9 11 a b c
=end pod
Jump to Line
Something went wrong with that request. Please try again.