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 ];
96 ## ::Indexed implementation
99 my ($array, $index) = @_;
104 my ($array, $index, $value) = @_;
105 $array->[$index] = $value;
109 my ($array, $index) = @_;
110 CORE::exists $array->[$index];
125 $array->keys->map(sub { [ $_, $array->[$_] ] });
129 my ($array, $sub) = @_;
130 for my $i (0 .. $#$array) {
131 $sub->($i, $array->[ $i ]);
136 my ($array, $sub) = @_;
137 $sub->($_) for (0 .. $#$array);
141 my ($array, $sub) = @_;
142 $sub->($_) for @$array;
146 my ($array, $n, $sub) = @_;
147 my $it = List::MoreUtils::natatime($n, @$array);
149 while (my @vals = $it->()) {
164 my $depth = CORE::pop @array;
165 --$depth if (defined($depth));
169 ? (defined($depth) && $depth == -1) ? $_ : _flatten_deep( @$_, $depth )
176 my ($array, $depth) = @_;
177 [ _flatten_deep(@$array, $depth) ];
184 return Perl6::Junction::all(@$array);
189 return Perl6::Junction::any(@$array);
194 return Perl6::Junction::none(@$array);
199 return Perl6::Junction::one(@$array);
204 sub print { CORE::print @{$_[0]} }
205 sub say { CORE::print @{$_[0]}, "\n" }
215 Moose::Autobox::Array - the Array role
221 [ 1..5 ]->isa('ARRAY'); # true
222 [ a..z ]->does('Moose::Autobox::Array'); # true
223 [ 0..2 ]->does('Moose::Autobox::List'); # true
225 print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
227 print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
229 print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true'
233 This is a role to describe operations on the Array type.
241 =item B<push ($value)>
245 =item B<unshift ($value)>
247 =item B<delete ($index)>
249 =item B<sprintf ($format_string)>
251 =item B<slice (@indices)>
255 =item B<flatten_deep ($depth)>
263 =head2 Indexed implementation
269 =item B<put ($index, $value)>
271 =item B<exists ($index)>
285 =item B<each_n_values ($n, $callback)>
289 =head2 List implementation
297 =item B<join (?$seperator)>
301 =item B<map (\&block)>
303 =item B<grep (\&block)>
305 Note that, in both the above, $_ is in scope within the code block, as well as
306 being passed as $_[0]. As per CORE::map and CORE::grep, $_ is an alias to
307 the list value, so can be used to to modify the list, viz:
312 $foo->map( sub {$_++} );
325 =item B<sort (?\&block)>
355 All complex software has bugs lurking in it, and this module is no
356 exception. If you find a bug please either email me, or add the bug
361 Stevan Little E<lt>stevan@iinteractive.comE<gt>
363 =head1 COPYRIGHT AND LICENSE
365 Copyright 2006-2008 by Infinity Interactive, Inc.
367 L<http://www.iinteractive.com>
369 This library is free software; you can redistribute it and/or modify
370 it under the same terms as Perl itself.