1 package Moose::Autobox::Array;
2 use Moose::Role 'with';
8 with 'Moose::Autobox::Ref',
9 'Moose::Autobox::List',
10 'Moose::Autobox::Indexed';
20 my ($array, @rest) = @_;
21 CORE::push @$array, @rest;
26 my ($array, @rest) = @_;
27 CORE::unshift @$array, @rest;
32 my ($array, $index) = @_;
33 CORE::delete $array->[$index];
42 my ($array, $indicies) = @_;
43 [ @{$array}[ @{$indicies} ] ];
47 # sprintf args need to be reversed,
48 # because the invocant is the array
49 sub sprintf { CORE::sprintf $_[1], @{$_[0]} }
51 ## ::List interface implementation
53 sub head { $_[0]->[0] }
54 sub tail { [ @{$_[0]}[ 1 .. $#{$_[0]} ] ] }
62 my ($array, $sub) = @_;
63 [ CORE::grep { $sub->($_) } @$array ];
67 my ($array, $sub) = @_;
68 [ CORE::map { $sub->($_) } @$array ];
72 my ($array, $sep) = @_;
74 CORE::join $sep, @$array;
79 [ CORE::reverse @$array ];
83 my ($array, $sub) = @_;
84 $sub ||= sub { $a cmp $b };
85 [ CORE::sort { $sub->($a, $b) } @$array ];
88 ## ::Indexed implementation
91 my ($array, $index) = @_;
96 my ($array, $index, $value) = @_;
97 $array->[$index] = $value;
101 my ($array, $index) = @_;
102 CORE::exists $array->[$index];
117 $array->keys->map(sub { [ $_, $array->[$_] ] });
121 my ($array, $sub) = @_;
122 for my $i (0 .. $#$array) {
123 $sub->($i, $array->[ $i ]);
128 my ($array, $sub) = @_;
129 for my $i (0 .. $#$array) {
135 my ($array, $sub) = @_;
136 $sub->($_) for @$array;
147 my $depth = CORE::pop @array;
148 --$depth if (defined($depth));
152 ? (defined($depth) && $depth == -1) ? $_ : _flatten_deep( @$_, $depth )
159 my ($array, $depth) = @_;
160 [ _flatten_deep(@$array, $depth) ];
167 return Perl6::Junction::all(@$array);
172 return Perl6::Junction::any(@$array);
177 return Perl6::Junction::none(@$array);
182 return Perl6::Junction::one(@$array);
187 sub print { CORE::print @{$_[0]} }
188 sub say { CORE::print @{$_[0]}, "\n" }
198 Moose::Autobox::Array - the Array role
204 [ 1..5 ]->isa('ARRAY'); # true
205 [ a..z ]->does('Moose::Autobox::Array'); # true
206 [ 0..2 ]->does('Moose::Autobox::List'); # true
208 print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
210 print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
212 print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true'
216 This is a role to describe operations on the Array type.
224 =item B<push ($value)>
228 =item B<unshift ($value)>
230 =item B<delete ($index)>
232 =item B<sprintf ($format_string)>
234 =item B<slice (@indices)>
238 =item B<flatten_deep ($depth)>
242 =head2 Indexed implementation
248 =item B<put ($index, $value)>
250 =item B<exists ($index)>
260 =head2 List implementation
268 =item B<join (?$seperator)>
272 =item B<map (\&block)>
274 =item B<grep (\&block)>
276 Note that, in both the above, $_ is in scope within the code block, as well as
277 being passed as $_[0]. As per CORE::map and CORE::grep, $_ is an alias to
278 the list value, so can be used to to modify the list, viz:
283 $foo->map( sub {$_++} );
296 =item B<sort (?\&block)>
326 All complex software has bugs lurking in it, and this module is no
327 exception. If you find a bug please either email me, or add the bug
332 Stevan Little E<lt>stevan@iinteractive.comE<gt>
334 =head1 COPYRIGHT AND LICENSE
336 Copyright 2006-2008 by Infinity Interactive, Inc.
338 L<http://www.iinteractive.com>
340 This library is free software; you can redistribute it and/or modify
341 it under the same terms as Perl itself.