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 $sub->($_) for (0 .. $#$array);
133 my ($array, $sub) = @_;
134 $sub->($_) for @$array;
138 my ($array, $n, $sub) = @_;
139 my $it = List::MoreUtils::natatime($n, @$array);
141 while (my @vals = $it->()) {
156 my $depth = CORE::pop @array;
157 --$depth if (defined($depth));
161 ? (defined($depth) && $depth == -1) ? $_ : _flatten_deep( @$_, $depth )
168 my ($array, $depth) = @_;
169 [ _flatten_deep(@$array, $depth) ];
176 return Perl6::Junction::all(@$array);
181 return Perl6::Junction::any(@$array);
186 return Perl6::Junction::none(@$array);
191 return Perl6::Junction::one(@$array);
196 sub print { CORE::print @{$_[0]} }
197 sub say { CORE::print @{$_[0]}, "\n" }
207 Moose::Autobox::Array - the Array role
213 [ 1..5 ]->isa('ARRAY'); # true
214 [ a..z ]->does('Moose::Autobox::Array'); # true
215 [ 0..2 ]->does('Moose::Autobox::List'); # true
217 print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
219 print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
221 print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true'
225 This is a role to describe operations on the Array type.
233 =item B<push ($value)>
237 =item B<unshift ($value)>
239 =item B<delete ($index)>
241 =item B<sprintf ($format_string)>
243 =item B<slice (@indices)>
247 =item B<flatten_deep ($depth)>
249 =item B<each_n_values ($n, $callback)>
253 =head2 Indexed implementation
259 =item B<put ($index, $value)>
261 =item B<exists ($index)>
277 =head2 List implementation
285 =item B<join (?$seperator)>
289 =item B<map (\&block)>
291 =item B<grep (\&block)>
293 Note that, in both the above, $_ is in scope within the code block, as well as
294 being passed as $_[0]. As per CORE::map and CORE::grep, $_ is an alias to
295 the list value, so can be used to to modify the list, viz:
300 $foo->map( sub {$_++} );
313 =item B<sort (?\&block)>
343 All complex software has bugs lurking in it, and this module is no
344 exception. If you find a bug please either email me, or add the bug
349 Stevan Little E<lt>stevan@iinteractive.comE<gt>
351 =head1 COPYRIGHT AND LICENSE
353 Copyright 2006-2008 by Infinity Interactive, Inc.
355 L<http://www.iinteractive.com>
357 This library is free software; you can redistribute it and/or modify
358 it under the same terms as Perl itself.