# class List

Sequence of values

```
my class List is Iterable does Positional { .. }
```

`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.

# Items, Flattening and Sigils

In Perl 6, assigning a `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 `Parcel`

or an `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
```

`.item`

can often be written as `$( ... )`

, and on an array variable even as `$@a`

.

# Methods

## routine elems

```
multi sub elems($list) returns Int:D
multi method elems(List:D:) returns Int:D
```

Returns the number of elements in the list.

## routine end

```
multi sub end($list) returns Int:D
multi method end(List:D:) returns Int:D
```

Returns the index of the last element.

## routine keys

```
multi sub keys($list) returns List:D
multi method keys(List:D:) returns List:D
```

Returns a list of indexes into the list (e.g., 0..(@list.elems-1)).

## routine values

```
multi sub values($list) returns List:D
multi method values(List:D:) returns List:D
```

Returns a copy of the list.

## routine 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
```

Returns

```
0, 'a', 1, 'b', 2, 'c'
```

## routine 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 values.

```
<a b c>.pairs # 0 => 'a', 1 => 'b', 2 => 'c'
```

## routine 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 `$separator`

and concatenates everything into a single string.

Example:

```
join ', ', <a b c>; # 'a, b, c'
```

## routine map

```
multi sub map(&code, *@elems) returns List:D
multi method map(List:D: &code) returns List:D
```

Invokes `&code`

for each element and gathers the return values in another list and returns it. This happens lazily, i.e. `&code`

is only invoked when the return values are accessed.

Examples:

```
> ('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
```

## routine 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 `$matcher`

smart-matches. The elements are returned in the order in which they appear in the original list.

Examples:

```
> ('hello', 1, 22/7, 42, 'world').grep: Int
1 42
> grep { .Str.chars > 3 }, 'hello', 1, 22/7, 42, 'world'
hello 3.142857 world
```

## routine grep-index

```
multi method grep-index(List:D: Mu $matcher) returns List:D
```

Returns a lazy list of indices against which the associated elements smart-match. The indicies are returned in order.

## routine 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 `$matcher`

, fails when no values match.

Examples:

```
say (1, 22/7, 42).first: * > 5; # 42
say $f = ('hello', 1, 22/7, 42, 'world').first: Complex;
say $f.perl; # Failure.new(exception => X::AdHoc.new(payload => "No values matched"))
```

## routine first-index

```
multi method first-index(List:D: Mu $matcher)
```

Returns the first index against which `$matcher`

smart-matches, or `Nil`

if no match was found.

## routine last-index

```
multi method last-index(List:D: Mu $matcher)
```

Returns the last index against which `$matcher`

smart-matches, or `Nil`

if no match was found.

## routine 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.

Example:

```
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
```

## method Bool

```
multi method Bool(List:D:) returns Bool:D
```

Returns `True`

if the list has at least one element, and `False`

for the empty list.

## method Str

```
multi method Str(List:D:) returns Str:D
```

Stringifies the elements of the list and joins them with spaces (same as `.join(' ')`

).

## method Int

```
multi method Int(List:D:) return Int:D
```

Returns the number of elements in the list (same as `.elems`

).

## method Numeric

```
multi method Numeric(List:D:) return Int:D
```

Returns the number of elements in the list (same as `.elems`

).

## routine pick

```
multi sub pick($count, *@list) returns List:D
multi method pick(List:D: $count = 1)
```

Returns `$count`

elements chosen at random and without repetition from the invocant. If `*`

is passed as `$count`

, or `$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.

Examples:

```
say <a b c d e>.pick; # b
b
say <a b c d e>.pick: 3; # c a e
say <a b c d e>.pick: *; # e d a b c
```

## routine roll

```
multi sub roll($count, *@list) returns List:D
multi method roll(List:D: $count = 1)
```

Returns a lazy list of `$count`

elements, each randomly selected from the list. Each random choice is made independently, like a separate die roll where each die face is a list element.

If `*`

is passed to `$count`

, returns a lazy, infinite list of randomly chosen elements from the original list.

Examples:

```
say <a b c d e>.roll; # b
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
```

## routine eager

```
multi method eager(List:D:) returns List:D
sub eager(*@elems) returns List:D
```

Evaluates all elements in the list eagerly, and returns them as a list. If a List signals that it is "known infinite", eager evaluation may stop at the point where the infinity is detected.

## routine 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 `reverse`

always refers to reversing elements of a list; to reverse the characters in a string, use flip.

Examples:

```
say <hello world!>.reverse # world! hello
say reverse ^10 # 9 8 7 6 5 4 3 2 1 0
```

## routine 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 `$n`

elements.

Examples:

```
<a b c d e>.rotate(2); # <c d e a b>
<a b c d e>.rotate(-1); # <e a b c d>
```

## routine 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 `infix:<cmp> `

is used for comparing list elements.

If `&by`

is provided, and it accepts two arguments, it is invoked for pairs of list elements, and should return `Order::Increase`

, `Order::Same`

or `Order::Decrease`

.

If `&by`

accepts only one argument, the list elements are sorted according to `by($a) cmp by($b) `

. The return values of `&by`

are cached, so that `&by`

is only called once per list element.

Examples:

```
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
```

## routine reduce

```
multi sub reduce(&with, *@elems)
multi method reduce(List:D: &with)
```

Applies `&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 `reduce`

is an implicit loop, and thus responds to `next`

, `last`

and `redo`

statements.

Example:

```
say (1, 2, 3).reduce: * - *; # -4
```

## routine splice

```
multi sub splice(@list, $start, $elems?, *@replacement) returns List:D
multi method splice(List:D: $start, $elems?, *@replacement) returns List:D
```

Deletes `$elems`

elements starting from index `$start`

from the list, returns them and replaces them by `@replacement`

. If `$elems`

is omitted, all the elements starting from index `$start`

are deleted.

Example:

```
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
```

## routine 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
```

## routine push

```
multi sub push(List:D, *@values) returns List:D
multi method push(List:D: *@values) returns List:D
```

Adds the `@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;
say @foo; # a b c 1 3 5 7 9 11
```

## routine 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>;
say @foo.shift; # a
say @foo.shift; # b
say @foo.shift; # Element shifted from empty list
```

## routine unshift

```
multi sub unshift(List:D, *@values) returns List:D
multi method unshift(List:D: *@values) returns List:D
```

Adds the `@values`

to the start of the list, and returns the modified list. Fails if `@values`

is infinite.

Example:

```
my @foo = <a b c>;
@foo.unshift: 1, 3 ... 11;
say @foo; # 1 3 5 7 9 11 a b c
```

## routine combinations

```
multi method combinations (List:D: Int:D $of) returns List:D
multi method combinations (List:D: Range:D $of = 0..*) returns List:D
multi sub combinations ($n, $k) returns List:D
```

The `Int`

variant returns all `$of`

-combinations of the invocant list. For example

```
say .join('|') for <a b c>.combinations(2);
```

prints

```
a|b
a|c
b|c
```

because all the 2-combinations of `'a', 'b', 'c'`

are `['a', 'b'], ['a', 'c'], ['b', 'c']`

.

The `Range`

variant combines all the individual combinations into a single list, so

```
say .join('|') for <a b c>.combinations(2..3);
```

prints

```
a|b
a|c
b|c
a|b|c
```

because that's the list of all 2- and 3-combinations.

The subroutine form `combinations($n, $k)`

is equivalent to `(^$n).combinations($k)`

, so

```
.say for combinations(4, 2)
```

prints

```
0 1
0 2
0 3
1 2
1 3
2 3
```

## routine permutations

```
multi method permutations(List:D:) returns List:D
multi sub permutations($n) returns List:D
```

Returns all possible permutations of a list as a list of arrays. So

```
say .join('|') for <a b c>.permutations
```

prints

```
a|b|c
a|c|b
b|a|c
b|c|a
c|a|b
c|b|a
```

`permutations`

treats all list elements as distinguishable, so `(1, 1, 2).permutations`

still returns a list of 6 elements, even though there are only three distinct permutations.

The subroutine form `permutations($n)`

is equivalent to `(^$n).permutations`

, so

```
.say for permutations 3;
```

prints

```
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
```

# Type graph

Below you should see a clickable image showing the type relations for List that links to the documentation pages for the related types. If not, try the PNG version instead.

# Methods supplied by role Positional

List does role Positional, which provides the following methods:

## method of

```
method of()
```

Returns the type constraint for elements of the positional container. Defaults to Mu.

# Methods supplied by class Cool

List inherits from class Cool, which provides the following methods:

## method conj

```
method conj()
```

Coerces the invocant to Numeric and returns the complex conjugate (that is, the number with the sign of the imaginary part negated).

```
say (1+2i).conj; # 1-2i
```

## method sign

```
method sign()
```

Coerces the invocant to Numeric and returns its sign, that is, 0 if the number is 0, 1 for positive and -1 for negative values.

## method rand

```
method rand()
```

Coerces the invocant to Num and returns a pseudo-random value between zero and the number.

## method sin

```
method sin()
```

Coerces the invocant to Numeric, interprets it as radians, returns its sine.

## method asin

```
method asin()
```

Coerces the invocant to Numeric, and returns its arc-sine in radians.

## method cos

```
method cos()
```

Coerces the invocant to Numeric, interprets it as radians, returns its sine.

## method acos

```
method acos()
```

Coerces the invocant to Numeric, and returns its arc-cosine in radians.

## method tan

Coerces the invocant to Numeric, interprets it as radians, returns its tangens.

## method atan

```
method atan()
```

Coerces the invocant to Numeric, and returns its arc-tangens in radians.

## method atan2

```
method atan2($y = 1e0)
```

Coerces the invocant to Numeric, and together with its argument, returns its two-argument arc-tangens in radians.

## method sec

```
method sec()
```

Coerces the invocant to Numeric, interprets it as radians, returns its secans, that is, the reciprocal of its cosine.

## method asec

```
method asec()
```

Coerces the invocant to Numeric, and returns its arc-secans in radians.

## method cosec

```
method cosec()
```

Coerces the invocant to Numeric, interprets it as radians, returns its cosecans, that is, the reciprocal of its sine.

## method acosec

```
method acosec()
```

Coerces the invocant to Numeric, and returns its arc-cosecans in radians.

## method cotan

```
method cotan()
```

Coerces the invocant to Numeric, interprets it as radians, returns its cotangens, that is, the reciprocal of its tangens.

## method acotan

```
method acotan()
```

Coerces the invocant to Numeric, and returns its arc-cotangens in radians.

## method sinh

```
method sinh()
```

Coerces the invocant to Numeric, and returns its Sine hyperbolicus.

## method asinh

```
method asinh()
```

Coerces the invocant to Numeric, and returns its Inverse Sine hyperbolicus.

## method cosh

```
method cosh()
```

Coerces the invocant to Numeric, and returns its Cosine hyperbolicus.

## method acosh

```
method acosh()
```

Coerces the invocant to Numeric, and returns its Inverse Cosine hyperbolicus.

## method tanh

```
method tanh()
```

Coerces the invocant to Numeric, and returns its Tangens hyperbolicus.

## method atanh

```
method atanh()
```

Coerces the invocant to Numeric, and returns its Inverse tangens hyperbolicus.

## method log

```
multi method log(Cool:D: Cool:D $base?)
```

Coerces the invocant to Numeric, and returns its Logarithm to base `$base`

, or to base `e`

(Euler's Number) if no base was supplied (Natural logarithm.

## method exp

```
multi method exp(Cool:D: Cool:D $base?)
```

Coerces the invocant to Numeric, and returns `$base`

raised to the power of this number. If no `$base`

is supplied, `e`

(Euler's Number) is used.

```
say 0.exp; # 1
say 1.exp; # 2.71828182845905
say 10.exp; # 22026.4657948067
```

## method round

```
multi method round(Cool:D: $unit = 1)
```

Coerces the invocant to Numeric, and rounds it to the unit of `$unit`

. If `$unit`

is 1, rounds to the nearest integer.

```
say 1.7.round; # 2
say 1.07.round(0.1); # 1.1
say 21.round(10); # 20
```

## method floor

```
multi method floor
```

Coerces the invocant to Numeric, and rounds it downwards to the nearest integer.

```
say "1.99".floor; # 1
say "-1.9".floor; # -2
say 0.floor; # 0
```

## method ceiling

```
multi method ceiling
```

Coerces the invocant to Numeric, and rounds it upwards to the nearest integer.

```
say "1".ceiling; # 1
say "-0.9".ceiling; # 0
say "42.1".ceiling; # 43
```

## method ord

```
method ord()
```

Coerces the invocant to Str, and returns the Unicode code point, number of the code point.

```
say 'a'.ord; # 65
```

The inverse operation is chr.

## method chr

```
method chr()
```

Coerces the invocant to Int, interprets it as a Unicode code points, and returns a string made of that code point.

```
say '65'.chr; # A
```

The inverse operation is ord.

Mnemonic: turns an integer into a *char*acter.

## method chars

```
method chars()
```

Coerces the invocant to Str, and returns the number of characters in the string. Characters should actually be grapheme clusters, though current implementation errornously count codepoints instead.

```
say 'møp'.chars; # 3
```

## method codes

```
method codes()
```

Coerces the invocant to Str, and returns the number of Unicode code points.

```
say 'møp'.codes; # 3
```

## method flip

```
method flip()
```

Coerces the invocant to Str, and returns a reversed version.

```
say 421.flip; # 124
```

## method trim

```
method trim()
```

Coerces the invocant to Str, and returns the string with both leading and trailing whitespace stripped.

```
my $stripped = ' abc '.trim;
say "<$stripped>"; # <abc>
```

## method trim-leading

```
method trim(-leading)
```

Coerces the invocant to Str, and returns the string with leading whitespace stripped.

```
my $stripped = ' abc '.trim-leading;
say "<$stripped>"; # <abc >
```

## method trim-trailing

```
method trim-trailing()
```

Coerces the invocant to Str, and returns the string with both leading and trailing whitespace stripped.

```
my $stripped = ' abc '.trim-trailing;
say "<$stripped>"; # < abc>
```

## method lc

```
method lc()
```

Coerces the invocant to Str, and returns it case-folded to lower case.

```
say "ABC".lc; # abc
```

## method uc

```
method uc()
```

Coerces the invocant to Str, and returns it case-folded to upper case (capital letters).

```
say "Abc".uc; # ABC
```

## method tc

```
method tc()
```

Coerces the invocant to Str, and returns it with the first letter case-folded to title case (or where not available, upper case).

```
say "abC".tc; # AbC
```

## method tclc

```
method tclc()
```

Coerces the invocant to Str, and returns it with the first letter case-folded to title case (or where not available, upper case), and the rest of the string case-folded to lower case..

```
say 'abC'.tclc; # Abc
```

## method wordcase

```
method wordcase(:&filter = &tclc, Mu :$where = True)
```

Coerces the invocant to Str, and filters each word that smart-matches against `$where`

through the `&filter`

. With the default filter (first character to upper case, rest to lower) and matcher (which accepts everything), this title-cases each word:

```
say "perl 6 programming".wordcase; # Perl 6 Programming
```

With a mather:

```
say "have fun working on perl".wordcase(:where({ .chars > 3 }));
# Have fun Working on Perl
```

With a customer filter too:

```
say "have fun working on perl".wordcase(:filter(&uc), :where({ .chars > 3 }));
# HAVE fun WORKING on PERL
```

## method chop

```
method chop()
```

Coerces the invocant to Str, and returns it with the last character removed.

```
say 'perl'.chop; # per
```

## method chomp

```
method chomp()
```

Coerces the invocant to Str, and returns it with the last character removed, if it is a logical newline.

```
say 'ab'.chomp.chars; # 2
say "a\n".chomp.chars; # 1
```

## method words

```
method words(Int() $limit)
```

Coerces the invocan to Str, and returns a list of words that make up the string (and if `$limit`

is supplied, only the first `$limit`

words).

```
say 'The quick brown fox'.words.join('|'); # The|quick|brown|fox
say 'The quick brown fox'.words(2).join('|'); # The|quick
```

Only whitespace counts as word boundaries

```
say "isn't, can't".words.join('|'); # isn't,|can't
```

## method IO

```
method IO() returns IO::Path:D
```

Coerces the invocant to IO::Path.

```
.say for '.'.IO.dir; # gives a directory listing
```

# Methods supplied by class Any

List inherits from class Any, which provides the following methods:

## method ACCEPTS

```
multi method ACCEPTS(Any:D: Mu $other)
```

Returns `True`

if `$other === self`

(i.e. it checks object identity).

## method any

Interprets the invocant as a list and creates an `any`

-Junction from it.

## method all

Interprets the invocant as a list and creates an `all`

-Junction from it.

## method one

Interprets the invocant as a list and creates an `one`

-Junction from it.

## method none

Interprets the invocant as a list and creates an `none`

-Junction from it.

## method list

Interprets the invocant as a list, and returns that list.

## method flat

Interprets the invocant as a list, flattens it, and returns that list.

```
say ((1, 2), (3)).elems; # 2
say ((1, 2), (3)).flat.elems; # 3
```

## method eager

Interprets the invocant as a list, evaluates it eagerly, and returns that list.

## method elems

Interprets the invocant as a list, and returns the number of elements in the list.

## method end

Interprets the invocant as a list, and returns the last index of that list.

# Methods supplied by class Mu

List inherits from class Mu, which provides the following methods:

## method Str

```
multi method Str() returns Str
```

Returns a string representation of the invocant, intended to be machine readable.

## method clone

```
method clone(*%twiddles)
```

Creates a shallow clone of the invocant. If named arguments are passed to it, their values are used in every place where an attribute name matches the name of a named argument.

## method new

```
multi method new(*%attrinit)
```

Default method for constructing (create + initialize) new objects of a class. This method expects only named arguments which are then used to initialize attributes with accessors of the same name.

Classes may provide their own `new`

method to override this default.

## method bless

```
method bless(*%attrinit) returns Mu:D
```

Lower-level object construction method than `new`

.

Creates a new object of the same type as the invocant, uses the named arguments to initialize attributes, and returns the created object.

You can use this method when writing custom constructors:

```
class Point {
has $.x;
has $.y;
multi method new($x, $y) {
self.bless(:$x, :$y);
}
}
my $p = Point.new(-1, 1);
```

(Though each time you write a custom constructor, remember that it makes subclassing harder).

## method CREATE

```
method CREATE() returns Mu:D
```

Allocates a new object of the same type as the invocant, without initializing any attributes.

## method print

```
multi method print() returns Bool:D
```

Prints value to `$*OUT`

after stringification using `.Str`

method without newline at end.

## method say

```
multi method say() returns Bool:D
```

Prints value to `$*OUT`

after stringification using `.gist`

method with newline at end.

## method ACCEPTS

```
multi method ACCEPTS(Mu:U: $other)
```

Performs a type check. Returns `True`

if `$other`

conforms to the invocant (which is always a type object or failure).

This is the method that is triggered on smart-matching against type objects, for example in `if $var ~~ Int { ... }`

.

## method WHICH

```
multi method WHICH() returns ObjAt:D
```

Returns an object of type ObjAt which uniquely identifies the object. Value types override this method which makes sure that two equivalent objects return the same return value from `WHICH`

.

## method WHERE

```
method WHERE() returns Int
```

Returns an `Int`

representing the memory address of the object.

## method WHY

```
multi method WHY()
```

Returns the attached Pod value. For instance,

```
sub cast(Spell $s)
#= Initiate a specified spell normally
#= (do not use for class 7 spells)
{
do-raw-magic($s);
}
say &cast.WHY;
```

prints

```
Initiate a specified spell normally (do not use for class 7 spells)
```

See the documentation specification for details about attaching Pod to variables, classes, functions, methods, etc.

This documentation was generated from List.pod.