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 # sprintf args need to be reversed,
43 # because the invocant is the array
44 sub sprintf { CORE::sprintf $_[1], @{$_[0]} }
46 ## ::List interface implementation
48 sub head { $_[0]->[0] }
49 sub tail { [ @{$_[0]}[ 1 .. $#{$_[0]} ] ] }
57 my ($array, $sub) = @_;
58 [ CORE::grep { $sub->($_) } @$array ];
62 my ($array, $sub) = @_;
63 [ CORE::map { $sub->($_) } @$array ];
67 my ($array, $sep) = @_;
69 CORE::join $sep, @$array;
74 [ CORE::reverse @$array ];
78 my ($array, $sub) = @_;
79 $sub ||= sub { $a cmp $b };
80 [ CORE::sort { $sub->($a, $b) } @$array ];
83 ## ::Indexed implementation
86 my ($array, $index) = @_;
91 my ($array, $index, $value) = @_;
92 $array->[$index] = $value;
96 my ($array, $index) = @_;
97 CORE::exists $array->[$index];
112 $array->keys->map(sub { [ $_, $array->[$_] ] });
119 return Perl6::Junction::All->all(@$array);
124 return Perl6::Junction::Any->any(@$array);
129 return Perl6::Junction::None->none(@$array);
134 return Perl6::Junction::One->one(@$array);
145 Moose::Autobox::Array - the Array role
152 [ 1..5 ]->isa('ARRAY'); # true
153 [ a..z ]->does('Moose::Autobox::Array'); # true
154 [ 0..2 ]->does('Moose::Autobox::List'); # true
156 print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
158 print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
160 print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true'
164 This is a role to describe operations on the Array type.
172 =item B<push ($value)>
176 =item B<unshift ($value)>
178 =item B<delete ($index)>
180 =item B<sprintf ($format_string)>
184 =head2 Indexed implementation
190 =item B<put ($index, $value)>
192 =item B<exists ($index)>
202 =head2 List implementation
210 =item B<join (?$seperator)>
214 =item B<map (\&block)>
216 =item B<grep (\&block)>
220 =item B<sort (?\&block)>
246 All complex software has bugs lurking in it, and this module is no
247 exception. If you find a bug please either email me, or add the bug
252 Stevan Little E<lt>stevan@iinteractive.comE<gt>
254 =head1 COPYRIGHT AND LICENSE
256 Copyright 2006 by Infinity Interactive, Inc.
258 L<http://www.iinteractive.com>
260 This library is free software; you can redistribute it and/or modify
261 it under the same terms as Perl itself.