is List
An Array
is a List
which forces all its elements to be scalar containers, which means you can assign to array elements.
Array
implements Positional
and as such provides support for subscripts.
Note from version 6.d, .raku
(.perl
before version 2019.11) can be called on multi-dimensional arrays.
If you want to declare an Array
of a specific type, you can do so using several different ways:
my of Int = 33,44; # [33 44]my is Array[Int] = 33,44; # [33 44]
The second example, which parameterizes a type, is only available from Rakudo 2019.03.
Methods§
method gist§
Exactly the same as List.gist
, except using square brackets for surrounding delimiters.
method pop§
method pop(Array:) is nodal
Removes and returns the last item from the array. Fails if the array is empty.
Like many Array
methods, method pop
may be called via the corresponding subroutine. For example:
my = <a b>; # a b.pop; # bpop ; # apop ;CATCH ;# OUTPUT: «X::Cannot::Empty: Cannot pop from an empty Array»
method push§
multi method push(Array: ** is raw --> Array)multi method push(Array: \value --> Array)multi method push(Array: Slip \values --> Array)
Adds the provided value or values to the end of the array, and returns the modified array. If any argument is a Slip
, method push
will add the values produced by the argument's iterator. It throws if the invocant array or a Slip
is lazy.
Example:
my = <a b c>;.push: 'd';say ; # OUTPUT: «[a b c d]»
Note that push
does not attempt to flatten its argument list. If you pass an array or list as the thing to push, it becomes one additional element:
my = <a b c>;my = <d e f>;.push: ;say .elems; # OUTPUT: «4»say [3].join; # OUTPUT: «def»
Multiple values are added to the array only if you supply them as separate arguments or in a Slip
:
my = '1';say .push: 'a', 'b'; # OUTPUT: «[1 a b]»my = <E F>;say .push: .Slip; # OUTPUT: «[1 a b E F]»
See method append if you want to append multiple values that are produced by a single non-slipping Iterable
.
method append§
multi method append(Array: ** is raw --> Array)multi method append(Array: \arg --> Array)
Adds the provided values to the end of the array and returns the modified array, or throws if the invocant array or an argument that requires flattening is lazy.
In contrast with method push, method append
adheres to the single argument rule and is probably best thought of as:
multi method append(Array: +values --> Array)
This means that if you pass a single argument that is a non-itemized Iterable
, append
will try to flatten it.
For example:
my = <a b c>;my = <d e f>;.append: ;say .elems; # OUTPUT: «6»say ; # OUTPUT: «[a b c d e f]»
method elems§
method elems(Array: --> Int)
Returns the number of elements in the invocant. Throws X::Cannot::Lazy
exception if the invocant is lazy. For shaped arrays, returns the outer dimension; see shape if you need information for all dimensions.
say [<foo bar ber>] .elems; # OUTPUT: «3»say (my [42;3;70]).elems; # OUTPUT: «42»try [-∞...∞].elems;say $!.^name; # OUTPUT: «X::Cannot::Lazy»
method clone§
method clone(Array: --> Array)
Clones the original Array
. Modifications of elements in the clone are not propagated to the original and vice-versa:
my = <a b c>; my = .clone;[1] = 42; .push: 72;say ; # OUTPUT: «[a 42 c]»say ; # OUTPUT: «[a b c 72]»
However, note that the reifier is shared between the two Arrays, so both Arrays will have the same elements even when each is randomly-generated on reification and each element will be reified just once, regardless of whether the reification was done by the clone or the original Array. Note: just as reifying an Array from multiple threads is not safe, so is, for example, reifying the clone from one thread while reifying the original from another thread is not safe.
my = 1, … ∞; my = .clone;say [^3]; # OUTPUT: «(1 0.0216426755282736 0.567660896142156)»say [^3]; # OUTPUT: «(1 0.0216426755282736 0.567660896142156)»
method flat§
multi method flat(Array:)multi method flat(Array:)
This method will return the type object itself if it's applied to a type object; when applied to an object, it will return a Seq
created from the Array
underlying iterator.
my = <a 2 c>;say .flat.^name; # OUTPUT: «Seq»
method shift§
method shift(Array:) is nodal
Removes and returns the first item from the array. Fails if the array is empty.
Example:
my = <a b>;say .shift; # OUTPUT: «a»say .shift; # OUTPUT: «b»say .shift;CATCH ;# OUTPUT: «X::Cannot::Empty: Cannot shift from an empty Array»
routine unshift§
multi unshift(Array, ** --> Array)multi method unshift(Array: ** --> Array)
Adds the @values
to the start of the array, and returns the modified array. Fails if @values
is a lazy list.
Example:
my = <a b c>;.unshift: 1, 3 ... 11;say ; # OUTPUT: «[(1 3 5 7 9 11) a b c]»
The notes in the documentation for method push apply, regarding how many elements are added to the array.
The routine prepend is the equivalent for adding multiple elements from one list or array.
routine prepend§
sub prepend(\array, |values)multi method prepend(Array: \values)multi method prepend(Array: ** is raw)
Adds the elements from values
to the front of the array, modifying it in-place.
Example:
my = <a b c>;.prepend: 1, 3 ... 11;say ; # OUTPUT: «[1 3 5 7 9 11 a b c]»
The difference from method unshift
is that if you prepend a single array or list argument, prepend
will flatten that array / list, whereas unshift
prepends the list / array as just a single element.
routine splice§
multi splice(, = 0, ?, * --> Array)multi method splice(Array: = 0, ?, * --> Array)
Deletes $elems
elements starting from index $start
from the Array
, returns them and replaces them by @replacement
. If $elems
is omitted or is larger than the number of elements starting from $start
, all the elements starting from index $start
are deleted. If both $start
and $elems
are omitted, all elements are deleted from the Array
and returned.
Each of $start
and $elems
can be specified as a Whatever
or as a Callable
that returns an Int
-compatible value: this returned value is then used as the corresponding argument to the splice
routine.
A Whatever
$start
uses the number of elements of @list
(or invocant). A Callable
$start
is called with one argument—the number of elements in @list
(or self
).
A Whatever
$elems
deletes from $start
to end of @list
(or self
) (same as no $elems
). A Callable
$elems
is called with one argument—the number of elements in @list
(or self
) minus the value of $start
.
Example:
my = <a b c d e f g>;say .splice(2, 3, <M N O P>); # OUTPUT: «[c d e]»say ; # OUTPUT: «[a b M N O P f g]»
It can be used to extend an array by simply splicing in more elements than the current size (since version 6.d)
my = <a b c d e f g>;say .splice(6, 4, <M N O P>); # OUTPUT: «[g]»say ; # OUTPUT: «[a b c d e f M N O P]»
The following equivalences hold (assuming that @a.elems ≥ $i
):
@a.push($x, $y) @a.splice: * , *, $x, $y @a.pop @a.splice: *-1, @a.shift @a.splice: 0 , 1, @a.unshift($x, $y) @a.splice: 0 , 0, $x, $y @a[$i] = $y @a.splice: $i , 1, $y,
As mentioned above, a Whatever
or Callable
object can be provided for both the $start
and $elems
parameters. For example, we could use either of them to remove the second to last element from an array provided it's large enough to have one:
my = <a b c d e f g>;say .splice: *-2, *-1; # OUTPUT: «[f]»say ; # OUTPUT: «[a b c d e g]»my = -> ;my = -> ;say .splice: , ; # OUTPUT: «[e]»say ; # OUTPUT: «[a b c d g]»
method shape§
method shape()
Returns the shape of the array as a list.
Example:
my [2;3] = ( < 1 2 3 >, < 4 5 6 > ); # Array with fixed dimensionssay .shape; # OUTPUT: «(2 3)»my = ( < 1 2 3 >, < 4 5 6 > ); # Normal array (of arrays)say .shape; # OUTPUT: «(*)»
method default§
method default
Returns the default value of the invocant, i.e. the value which is returned when trying to access an element in the Array
which has not been previously initialized or when accessing an element which has explicitly been set to Nil
. Unless the Array
is declared as having a default value by using the is default trait the method returns the type object for Any
.
my = 1, "two", 2.718;say .default; # OUTPUT: «(Any)»say [4]; # OUTPUT: «(Any)»my is default(17) = 1, "two", 3;say .default; # OUTPUT: «17»say [4]; # OUTPUT: «17»[1] = Nil; # (resets element to its default)say [1]; # OUTPUT: «17»
method of§
method of()
Returns the type constraint for the values of the invocant. By default, i.e. if no type constraint is given during declaration, the method returns (Mu)
.
my = 1, 'two', 3.14159; # (no type constraint specified)say .of; # OUTPUT: «(Mu)»my Int = 1, 2, 3; # (values must be of type Int)say .of; # OUTPUT: «(Int)».push: 'd';CATCH ;# OUTPUT: «X::TypeCheck::Assignment: Type check failed in assignment to @a2; expected Int but got Str ("d")»
method dynamic§
method dynamic(Array: --> Bool)
Returns True
if the invocant has been declared with the is dynamic trait, that is, if it's a dynamic variable that can be accessed from the inner lexical scope without having been declared there.
my ;say .dynamic; # OUTPUT: «False»my is dynamic;say .dynamic; # OUTPUT: «True»
If you declare a variable with the *
twigil is dynamic
is implied.
my ;say .dynamic; # OUTPUT: «True»
Please note that the dynamic
trait is a property of the variable, not the content. If a Scalar
dynamic variable contains an array, rules for this container will apply (and it will always return False
).
method List§
multi method List(Array:)
Converts the array to a List
.
my = [1];[3]=3;say .List; # OUTPUT: «(1 Nil Nil 3)»
The holes will show up as Nil
.
method Slip§
multi method Slip(Array: --> Slip)
Converts the array to a Slip
, filling the holes with the type value the Array
has been defined with.
my Int = [0];[3]=3;say .Slip; # OUTPUT: «(0 (Int) (Int) 3)»