develooper Front page | perl.perl6.language | Postings from July 2006

List operations for S29

Aaron Sherman
July 7, 2006 07:45
List operations for S29
Message ID:
I've worked over the basic list operations in S29 (see below, as I can't
commit). The big shocker is going to be this: splice can't be as
full-featured as we wanted. According to audreyt on #perl6, Perl 6.0.0
cannot expect to have MMD on both the inbound and outbound signature.
Given that, we'll have to replace:

	splice(@foo,1,5) = 1..^$end;


	splice(@foo,1,5) <== 1..^$end;

I'm also doing away with sub wherever I can, since all core functions
probably need to be "multi sub" there's no reason to be explicitly
redundant. S06 tells us that "sub" is the default when multi or proto
are provided.

Here's the full text for the changed section:

=item pop

 our Scalar multi Array::pop ( @array is rw )
 our Scalar multi method Array::pop ( @array: )

Remove the last element of C<@array> and return it.

=item push

 our Int multi Array::push ( @array is rw, *@values )
 our Int multi mehtod Array::push ( @array: *@values )

Add to the end of C<@array>, all of the subsequent arguments.

=item shift

 our Scalar multi Array::shift ( @array is rw  )
 our Scalar multi method Array::shift ( @array:  )

Remove the first element from C<@array> and return it.

=item splice

 our List multi Array::splice( @array is rw, Int $offset = 0, Int $size?, *@values )

C<splice> fills many niches in array-management, but its fundamental behavior
is to remove zero or more elements from an array and replace them with a
new (and potentially empty) list. This operation can shorten or lengthen
the target array.

C<$offset> is the index of the array element to start with. It defaults
to C<0>.

C<$size> is the number of elements to remove from C<@array>. It defaults
to removing the rest of the array from C<$offset> on.

The slurpy list of values (if any) is then inserted at C<$offset>.

Calling splice with a traditional parameter list, you must define C<$offset>
and C<$size> if you wish to pass a replacement list of values. To avoid
having to pass these otherwise optional parameters, use the piping operator(s):

 splice(@array,10) <== 1..*;

which replaces C<@array[10]> and all subsequent elements with an infinite
series starting at C<1>.

This behaves similarly to Perl 5's C<splice>.

If C<@array> is multidimensional, C<splice> operates only on the first
dimension, and works with Array References.

=item unshift

 our Int multi Array::unshift ( @array is rw, *@values )
 our Int multi method Array::unshift ( @array: *@values )

C<unshift> adds the values onto the start of the C<@array>.

Aaron Sherman <>
Senior Systems Engineer and Toolsmith
"We had some good machines, but they don't work no more." -Shriekback Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About