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->[$_] ] });
124 return Perl6::Junction::All->all(@$array);
129 return Perl6::Junction::Any->any(@$array);
134 return Perl6::Junction::None->none(@$array);
139 return Perl6::Junction::One->one(@$array);
150 Moose::Autobox::Array - the Array role
156 [ 1..5 ]->isa('ARRAY'); # true
157 [ a..z ]->does('Moose::Autobox::Array'); # true
158 [ 0..2 ]->does('Moose::Autobox::List'); # true
160 print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
162 print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
164 print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true'
168 This is a role to describe operations on the Array type.
176 =item B<push ($value)>
180 =item B<unshift ($value)>
182 =item B<delete ($index)>
184 =item B<sprintf ($format_string)>
186 =item B<slice (@indices)>
190 =head2 Indexed implementation
196 =item B<put ($index, $value)>
198 =item B<exists ($index)>
208 =head2 List implementation
216 =item B<join (?$seperator)>
220 =item B<map (\&block)>
222 =item B<grep (\&block)>
226 =item B<sort (?\&block)>
252 All complex software has bugs lurking in it, and this module is no
253 exception. If you find a bug please either email me, or add the bug
258 Stevan Little E<lt>stevan@iinteractive.comE<gt>
260 =head1 COPYRIGHT AND LICENSE
262 Copyright 2006 by Infinity Interactive, Inc.
264 L<http://www.iinteractive.com>
266 This library is free software; you can redistribute it and/or modify
267 it under the same terms as Perl itself.