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;
153 my $depth = CORE::pop @array;
154 --$depth if (defined($depth));
158 ? (defined($depth) && $depth == -1) ? $_ : _flatten_deep( @$_, $depth )
165 my ($array, $depth) = @_;
166 [ _flatten_deep(@$array, $depth) ];
173 return Perl6::Junction::all(@$array);
178 return Perl6::Junction::any(@$array);
183 return Perl6::Junction::none(@$array);
188 return Perl6::Junction::one(@$array);
193 sub print { CORE::print @{$_[0]} }
194 sub say { CORE::print @{$_[0]}, "\n" }
204 Moose::Autobox::Array - the Array role
210 [ 1..5 ]->isa('ARRAY'); # true
211 [ a..z ]->does('Moose::Autobox::Array'); # true
212 [ 0..2 ]->does('Moose::Autobox::List'); # true
214 print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
216 print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
218 print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true'
222 This is a role to describe operations on the Array type.
230 =item B<push ($value)>
234 =item B<unshift ($value)>
236 =item B<delete ($index)>
238 =item B<sprintf ($format_string)>
240 =item B<slice (@indices)>
244 =item B<flatten_deep ($depth)>
252 =head2 Indexed implementation
258 =item B<put ($index, $value)>
260 =item B<exists ($index)>
276 =head2 List implementation
284 =item B<join (?$seperator)>
288 =item B<map (\&block)>
290 =item B<grep (\&block)>
292 Note that, in both the above, $_ is in scope within the code block, as well as
293 being passed as $_[0]. As per CORE::map and CORE::grep, $_ is an alias to
294 the list value, so can be used to to modify the list, viz:
299 $foo->map( sub {$_++} );
312 =item B<sort (?\&block)>
342 All complex software has bugs lurking in it, and this module is no
343 exception. If you find a bug please either email me, or add the bug
348 Stevan Little E<lt>stevan@iinteractive.comE<gt>
350 =head1 COPYRIGHT AND LICENSE
352 Copyright 2006-2008 by Infinity Interactive, Inc.
354 L<http://www.iinteractive.com>
356 This library is free software; you can redistribute it and/or modify
357 it under the same terms as Perl itself.