routine append

Documentation for routine append assembled from the following types:

class Any

From Any

(Any) method append

Defined as:

multi method append(Any:U \SELF: |values)

In the case the instance is not a positional-thing, it instantiates it as a new Array, otherwise clone the current instance. After that, it appends the values passed as arguments to the array obtained calling Array.append on it.

my $a;
say $a.append# OUTPUT: «[]␤» 
my $b;
say $b.append((1,2,3)); # OUTPUT: «[1 2 3]␤»

language documentation Independent routines

From Independent routines

(Independent routines) sub append

Defined as:

multi sub append(\a, \b)
multi sub append(\a**@b)

Probably best thought of as a simple:

sub append(\a+b)

Calls method append on container a which is supposed to add b or the values in @b to the end of a container. In contrast with sub push, the container method is supposed to always add the values produced by the iterator if called with a single Iterable that is not inside a Scalar container.

An example of where this subroutine may be useful is when appending to the values of a Hash. Whereas method append will silently ignore literal pairs that are interpreted as named arguments, sub append will throw:

my %h = => 0;
append %h=> (142);
CATCH { default { put .message } };
# OUTPUT: «Unexpected named argument 'i' passed␤»

class Nil

From Nil

(Nil) method append

method append(*@)

Warns the user that they tried to append onto a Nil (or derived type object).

class Array

From Array

(Array) method append

Defined as

multi method append(Array:D: \value --> Array:D)
multi method append(Array:D: **@values is raw --> Array:D)

A subclass that just wants to accept the same arguments as Array can override all candidates with a single:

method append(::?CLASS:D: +values --> ::?CLASS:D)

The Array class adds the provided values to the end of the array, and returns the modified array. It throws if the invocant array or an argument that requires flattening is lazy.

The difference with method method push is that if you append a single non-itemized Iterable, append will try to flatten it. For example:

my @a = <a b c>;
my @b = <d e f>;
@a.append: @b;
say @a.elems;               # OUTPUT: «6␤» 
say @a;                     # OUTPUT: «[a b c d e f]␤»

class Hash

From Hash

(Hash) method append

Defined as:

method append(+@values)

Append the provided Pairs or even sized list to the Hash. If a key already exists, turn the existing value into an Array and push new value onto that Array. Please note that you can't mix even sized lists and lists of Pairs. Also, bare Pairs or colon pairs will be treated as named arguments to .append.

my %h = => 1;
%h.append('b'2'c'3);
%h.append( %(=> 4) );
say %h;
# OUTPUT: «{a => 1, b => 2, c => 3, d => 4}␤» 
%h.append('a'2);
# OUTPUT: «{{a => [1 2], b => 2, c => 3, d => 4}␤»

Note: Compared to push, append will slip in the given value, whereas push will add it as is:

my %hb = :a[42, ]; %hb.append: "a" => <a b c a>;
say %hb# OUTPUT: «{a => [42 a b c a]}␤» 
 
my %ha = :a[42, ]; %ha.push: "a" => <a b c a>;
say %ha# OUTPUT: «{a => [42 (a b c a)]}␤»

role Buf

From Buf

(Buf) method append

method append$elems )

Appends at the end of the buffer

$.append@φ[5..10] );
say $.raku# OUTPUT: «Buf.new(1,1,2,3,5,8,13,21,34,55,89)»