Documentation for routine push
assembled from the following pages:
Class: Any §
From Any
(Any) method push §
Defined as:
multi method push(Any \SELF: |values --> Positional)
The method push is defined for undefined invocants and allows for autovivifying undefined to an empty Array, unless the undefined value implements Positional already. The argument provided will then be pushed into the newly created Array.
my ;say <a>; # OUTPUT: «(Any)» <-- Undefined <a>.push(1); # .push on Any say ; # OUTPUT: «{a => [1]}» <-- Note the Array
Class: Nil §
From Nil
(Nil) method push §
method push(*@)
Warns the user that they tried to push onto a Nil
or derived type object.
Role: Buf §
From Buf
(Buf) method push §
method push( )
Adds elements at the end of the buffer
my = 1,1, * + * … ∞;my = Buf.new( [^5] );.push( [5] );say .raku; # OUTPUT: «Buf.new(1,1,2,3,5,8)»
Class: Hash §
From Hash
(Hash) method push §
Defined as:
method push(Hash: +new)
Adds the new
elements to the hash with the same semantics as hash assignment, but with three exceptions:
The hash isn't emptied first, i.e. old pairs are not deleted.
If a key already exists in the hash, and the corresponding value is an Array, the new value is pushed onto the array (instead of replacing it).
If a key already exists in the hash, and the corresponding value is not an Array, old and new value are both placed into an array in the place of the old value.
Example:
my = a => 1;.push: (a => 1); # a => [1,1] .push: (a => 1) xx 3 ; # a => [1,1,1,1,1] .push: (b => 3); # a => [1,1,1,1,1], b => 3 .push('c' => 4); # a => [1,1,1,1,1], b => 3, c => 4 push , 'd' => 5; # a => [1,1,1,1,1], b => 3, c => 4, d => 5
Please note that literal pairs in the argument list may be interpreted as named arguments and as such won't end up in the Hash
:
my .= push(e => 6);say .raku; # OUTPUT: «{}»
Use the corresponding subroutine to catch this kind of mistake:
push my , f => 7;CATCH ;# OUTPUT: «Unexpected named argument 'f' passed»
Also note that push can be used as a replacement for assignment during hash initialization very useful ways. Take for instance the case of an inverted index:
my = 'hash' => 323, 'pair' => 322, 'pipe' => 323;(my ).push: .invert;say ; # OUTPUT: «{322 => pair, 323 => [pipe hash]}»
Note that such an initialization could also be written as
my = 'hash' => 323, 'pair' => 322, 'pipe' => 323;my .= push: .invert;
Note: Compared to append
, push
will add the given value as is, whereas append
will slip
it in:
my = :a[42, ]; .push: "a" => <a b c a>;say ; # OUTPUT: «{a => [42 (a b c a)]}» my = :a[42, ]; .append: "a" => <a b c a>;say ; # OUTPUT: «{a => [42 a b c a]}»
Language documentation: Independent routines §
From Independent routines
(Independent routines) sub push §
Defined as:
multi sub push(\a, ** is raw)multi sub push(\a, \b)
Calls method push
on the first argument, passing the remaining arguments. Method push
is supposed to add the provided values to the end of the collection or parts thereof. See the documentation of the Hash
method for an example where indirection via this subroutine can be helpful.
The push
method is supposed to flatten all arguments of type Slip
. Therefore, if you want to implement a conforming method for a new collection type, it should behave as if its signature was just:
multi method push(::?CLASS: ** is raw --> ::?CLASS)
Autovivification to an instance of the new type is provided by the default base class if the new type implements the Positional
role. If the new type is not Positional
, autovivification can be supported by an adding a multi method with a signature like
multi method push(::?CLASS: ** is raw --> ::?CLASS)
Class: Array §
From Array
(Array) method push §
Defined as:
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
.