1 package Moose::Autobox::Array;
2 use Moose::Role 'with';
5 use Syntax::Keyword::Junction::All ();
6 use Syntax::Keyword::Junction::Any ();
7 use Syntax::Keyword::Junction::None ();
8 use Syntax::Keyword::Junction::One ();
10 our $VERSION = '0.14';
12 with 'Moose::Autobox::Ref',
13 'Moose::Autobox::List',
14 'Moose::Autobox::Indexed';
24 my ($array, @rest) = @_;
25 CORE::push @$array, @rest;
30 my ($array, @rest) = @_;
31 CORE::unshift @$array, @rest;
36 my ($array, $index) = @_;
37 CORE::delete $array->[$index];
46 my ($array, $indicies) = @_;
47 [ @{$array}[ @{$indicies} ] ];
51 # sprintf args need to be reversed,
52 # because the invocant is the array
53 sub sprintf { CORE::sprintf $_[1], @{$_[0]} }
55 ## ::List interface implementation
57 sub head { $_[0]->[0] }
58 sub tail { [ @{$_[0]}[ 1 .. $#{$_[0]} ] ] }
66 my ($array, $sub) = @_;
67 [ CORE::grep { $sub->($_) } @$array ];
71 my ($array, $sub) = @_;
72 [ CORE::map { $sub->($_) } @$array ];
76 my ($array, $sep) = @_;
78 CORE::join $sep, @$array;
83 [ CORE::reverse @$array ];
87 my ($array, $sub) = @_;
88 $sub ||= sub { $a cmp $b };
89 [ CORE::sort { $sub->($a, $b) } @$array ];
100 ## ::Indexed implementation
103 my ($array, $index) = @_;
108 my ($array, $index, $value) = @_;
109 $array->[$index] = $value;
113 my ($array, $index) = @_;
114 CORE::exists $array->[$index];
129 $array->keys->map(sub { [ $_, $array->[$_] ] });
133 my ($array, $sub) = @_;
134 for my $i (0 .. $#$array) {
135 $sub->($i, $array->[ $i ]);
140 my ($array, $sub) = @_;
141 $sub->($_) for (0 .. $#$array);
145 my ($array, $sub) = @_;
146 $sub->($_) for @$array;
150 my ($array, $n, $sub) = @_;
151 my $it = List::MoreUtils::natatime($n, @$array);
153 while (my @vals = $it->()) {
168 my $depth = CORE::pop @array;
169 --$depth if (defined($depth));
173 ? (defined($depth) && $depth == -1) ? $_ : _flatten_deep( @$_, $depth )
180 my ($array, $depth) = @_;
181 [ _flatten_deep(@$array, $depth) ];
188 return Syntax::Keyword::Junction::All->new(@$array);
193 return Syntax::Keyword::Junction::Any->new(@$array);
198 return Syntax::Keyword::Junction::None->new(@$array);
203 return Syntax::Keyword::Junction::One->new(@$array);
208 sub print { CORE::print @{$_[0]} }
209 sub say { CORE::print @{$_[0]}, "\n" }
221 Moose::Autobox::Array - the Array role
227 [ 1..5 ]->isa('ARRAY'); # true
228 [ a..z ]->does('Moose::Autobox::Array'); # true
229 [ 0..2 ]->does('Moose::Autobox::List'); # true
231 print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
233 print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
235 print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true'
239 This is a role to describe operations on the Array type.
247 =item B<push ($value)>
251 =item B<unshift ($value)>
253 =item B<delete ($index)>
255 =item B<sprintf ($format_string)>
257 =item B<slice (@indices)>
261 =item B<flatten_deep ($depth)>
269 =head2 Indexed implementation
275 =item B<put ($index, $value)>
277 =item B<exists ($index)>
291 =item B<each_n_values ($n, $callback)>
295 =head2 List implementation
303 =item B<join (?$seperator)>
307 =item B<map (\&block)>
309 =item B<grep (\&block)>
311 Note that, in both the above, $_ is in scope within the code block, as well as
312 being passed as $_[0]. As per CORE::map and CORE::grep, $_ is an alias to
313 the list value, so can be used to modify the list, viz:
318 $foo->map( sub {$_++} );
331 =item B<sort (?\&block)>
361 All complex software has bugs lurking in it, and this module is no
362 exception. If you find a bug please either email me, or add the bug
367 Stevan Little E<lt>stevan@iinteractive.comE<gt>
369 =head1 COPYRIGHT AND LICENSE
371 Copyright 2006-2008 by Infinity Interactive, Inc.
373 L<http://www.iinteractive.com>
375 This library is free software; you can redistribute it and/or modify
376 it under the same terms as Perl itself.