Arrays: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: further exposition on arrays in Perl 6)
(→‎Some further exposition:: Reduce the hand-waving and outright lies a bit.)
Line 3,956: Line 3,956:
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.
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.


Values may be pushed onto the end of an array, popped off of the end, shifted off of the front or unshifted onto the front.
Values may be pushed onto the end of an array, popped off of the end, shifted off of the front or unshifted onto the front, and spliced into or out of the interior.
@array.push: 'value';
@array.push: 'value';
my $value = @array.pop;
my $value = @array.pop;
@array.unshift: 'value';
@array.unshift: 'value';
my $value = @array.shift;
my $value = @array.shift;
@array.splice(2,3, <some arbitrary string values>);


Arrays may be constrained to only accept a certain number of objects or only a certain type of object.
Arrays may be constrained to only accept a certain number of objects or only a certain type of object.
Line 3,989: Line 3,990:
@array[1;1] # same thing, implies rectangular (non-ragged) arrays
@array[1;1] # same thing, implies rectangular (non-ragged) arrays


There are several constructs that do the Positional Role similar to arrays. They are ordered and use integer indexing and are often used in similar circumstances, however, they are immutable. Values in slots can not be changed. They can not be pushed to or popped from.
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 6 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 collection of immutable values. Lists are constructed similarly to arrays:
'''List''': A fixed Iterable collection of immutable values. Lists are constructed similarly to arrays:
(1, 2, 3, 4)
(1, 2, 3, 4)
('a', 'b', 'c', 'd')
('a', 'b', 'c', 'd')
Line 4,011: Line 4,012:
1,1,{$^n2 + $^n1 + 1} ... * # infinite Leonardo numbers
1,1,{$^n2 + $^n1 + 1} ... * # infinite Leonardo numbers


Postcircumfix indexing works for any object that has a Positional role, it need not be in a @-sigiled variable, or indeed, in a variable at all.
Postcircumfix indexing works for any object that has a Positional (or PositionalBindFailover) role, it need not be in a @-sigiled variable, or indeed, in a variable at all.
[2,4,6,8,10][1] # 4 - anonymous array
[2,4,6,8,10][1] # 4 - anonymous array
<my dog has fleas>[*-2] # 'has' - anonymous list
<my dog has fleas>[*-2] # 'has' - anonymous list