1 package Moose::Autobox::Array;
2 use Moose::Role 'with';
7 with 'Moose::Autobox::Ref',
8 'Moose::Autobox::List',
9 'Moose::Autobox::Indexed';
19 my ($array, @rest) = @_;
20 CORE::push @$array, @rest;
25 my ($array, @rest) = @_;
26 CORE::unshift @$array, @rest;
31 my ($array, $index) = @_;
32 CORE::delete $array->[$index];
41 # sprintf args need to be reversed,
42 # because the invocant is the array
43 sub sprintf { CORE::sprintf $_[1], @{$_[0]} }
45 ## ::List interface implementation
47 sub head { $_[0]->[0] }
48 sub tail { [ @{$_[0]}[ 1 .. $#{$_[0]} ] ] }
56 my ($array, $sub) = @_;
57 [ CORE::grep { $sub->($_) } @$array ];
61 my ($array, $sub) = @_;
62 [ CORE::map { $sub->($_) } @$array ];
66 my ($array, $sep) = @_;
68 CORE::join $sep, @$array;
73 [ CORE::reverse @$array ];
77 my ($array, $sub) = @_;
78 $sub ||= sub { $a cmp $b };
79 [ CORE::sort { $sub->($a, $b) } @$array ];
82 ## ::Indexed implementation
85 my ($array, $index) = @_;
90 my ($array, $index, $value) = @_;
91 $array->[$index] = $value;
95 my ($array, $index) = @_;
96 CORE::exists $array->[$index];
111 $array->keys->map(sub { [ $_, $array->[$_] ] });
122 Moose::Autobox::Array - the Array role
129 [ 1..5 ]->isa('ARRAY'); # true
130 [ a..z ]->does('Moose::Autobox::Array'); # true
131 [ 0..2 ]->does('Moose::Autobox::List'); # true
133 print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
135 print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
139 This is a role to describe operations on the Array type.
147 =item B<push ($value)>
151 =item B<unshift ($value)>
153 =item B<delete ($index)>
155 =item B<sprintf ($format_string)>
159 =head2 Indexed implementation
165 =item B<put ($index, $value)>
167 =item B<exists ($index)>
177 =head2 List implementation
185 =item B<join (?$seperator)>
189 =item B<map (\&block)>
191 =item B<grep (\&block)>
195 =item B<sort (?\&block)>
207 All complex software has bugs lurking in it, and this module is no
208 exception. If you find a bug please either email me, or add the bug
213 Stevan Little E<lt>stevan@iinteractive.comE<gt>
215 =head1 COPYRIGHT AND LICENSE
217 Copyright 2006 by Infinity Interactive, Inc.
219 L<http://www.iinteractive.com>
221 This library is free software; you can redistribute it and/or modify
222 it under the same terms as Perl itself.