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->[$_] ] });
128 return Perl6::Junction::all(@$array);
133 return Perl6::Junction::any(@$array);
138 return Perl6::Junction::none(@$array);
143 return Perl6::Junction::one(@$array);
148 sub print { CORE::print @{$_[0]} }
149 sub say { CORE::print @{$_[0]}, "\n" }
159 Moose::Autobox::Array - the Array role
165 [ 1..5 ]->isa('ARRAY'); # true
166 [ a..z ]->does('Moose::Autobox::Array'); # true
167 [ 0..2 ]->does('Moose::Autobox::List'); # true
169 print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
171 print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
173 print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true'
177 This is a role to describe operations on the Array type.
185 =item B<push ($value)>
189 =item B<unshift ($value)>
191 =item B<delete ($index)>
193 =item B<sprintf ($format_string)>
195 =item B<slice (@indices)>
201 =head2 Indexed implementation
207 =item B<put ($index, $value)>
209 =item B<exists ($index)>
219 =head2 List implementation
227 =item B<join (?$seperator)>
231 =item B<map (\&block)>
233 =item B<grep (\&block)>
237 =item B<sort (?\&block)>
267 All complex software has bugs lurking in it, and this module is no
268 exception. If you find a bug please either email me, or add the bug
273 Stevan Little E<lt>stevan@iinteractive.comE<gt>
275 =head1 COPYRIGHT AND LICENSE
277 Copyright 2006-2008 by Infinity Interactive, Inc.
279 L<http://www.iinteractive.com>
281 This library is free software; you can redistribute it and/or modify
282 it under the same terms as Perl itself.