Jump to content

Arrays: Difference between revisions

10 bytes removed ,  4 years ago
m
Perl6 -> Raku
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
m (Perl6 -> Raku)
Line 5,502:
=={{header|Raku}}==
(formerly Perl 6)
At its most basic, an array in Perl 6Raku is quite similar to an array in Perl 5.
 
<lang perl6>my @arr;
Line 5,515:
===Some further exposition:===
 
In Perl 6Raku, arrays have a very specific definition: "A collection of Scalar containers that do the Positional Role." Scalar container means it is mutable and may contain any object; an Integer, a Rational number, a String, another Array, whatever... literally any other object that can be instantiated in Perl 6Raku. The Positional Role means that it uses integer indexing for access. The index must be a positive integer, an expression that evaluates to a positive integer, or something that can be coerced to a positive integer. Arrays are always indexed from 0. The starting index can not be changed.
 
Arrays are unconstrained by default. They may hold any number of any type of object up to available memory. They do not need to be pre-allocated. Simply assigning (or even referring in some cases) to an index slot is enough to autovivify the container and allocate enough memory to hold the assigned object. Memory will automatically be allocated and will grow and shrink as necessary to hold the values assigned.
Line 5,537:
my @implicit = <yep, this too>
 
Array variables in Perl 6Raku are variables whose names bear the @ sigil, and are expected to contain some sort of list-like object. Of course, other variables may also contain these objects, but @-sigiled variables always do, and are expected to act the part. Array storage slots are accessed through postcircumfix square bracket notation. Unlike Perl 5, @-sigiled variables are invariant on access, whether you are accessing one slot, many slots, or all of the slots. The first slot in @array is @array[0] not $array[0]. @array and $array are two different unconnected variables.
@array[1] # a single value in the 2nd slot
@array[*-1] # a single value in the last slot
Line 5,553:
@array[1;1] # same thing, implies rectangular (non-ragged) arrays
 
There are several objects that have an Iterable Role and a PositionalBindFailover Role which makes them act similar to arrays and allows them to be used nearly interchangeably in read-only applications. (Perl 6Raku is big on duck typing. "If it looks like a duck and quacks like a duck and waddles like a duck, it's a duck.") These constructs are ordered and use integer indexing and are often used in similar circumstances as arrays, however, '''they are immutable'''. Values in slots can not be changed. They can not be pushed to, popped from or spliced. They can easily converted to arrays by simply assigning them to an array variable.
 
'''List''': A fixed Iterable collection of immutable values. Lists are constructed similarly to arrays:
10,339

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.