Add test for modify-in-place usage of ->map, as well as an example in the pod docs.
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Array.pm
CommitLineData
5f654d8e 1package Moose::Autobox::Array;
2use Moose::Role 'with';
7fc99864 3use Perl6::Junction;
7dad2765 4use Moose::Autobox;
5f654d8e 5
7dad2765 6our $VERSION = '0.03';
5f654d8e 7
e6bb88b0 8with 'Moose::Autobox::Ref',
31d40d73 9 'Moose::Autobox::List',
10 'Moose::Autobox::Indexed';
6cf5bcf2 11
12## Array Interface
13
14sub pop {
15 my ($array) = @_;
16 CORE::pop @$array;
17}
18
19sub push {
20 my ($array, @rest) = @_;
21 CORE::push @$array, @rest;
22 $array;
23}
24
25sub unshift {
26 my ($array, @rest) = @_;
27 CORE::unshift @$array, @rest;
28 $array;
29}
5f654d8e 30
6cf5bcf2 31sub delete {
32 my ($array, $index) = @_;
33 CORE::delete $array->[$index];
34}
35
36sub shift {
37 my ($array) = @_;
38 CORE::shift @$array;
c11e6a74 39}
40
41sub slice {
42 my ($array, $indicies) = @_;
43 [ @{$array}[ @{$indicies} ] ];
44}
6cf5bcf2 45
46# NOTE:
47# sprintf args need to be reversed,
48# because the invocant is the array
49sub sprintf { CORE::sprintf $_[1], @{$_[0]} }
50
51## ::List interface implementation
52
53sub head { $_[0]->[0] }
54sub tail { [ @{$_[0]}[ 1 .. $#{$_[0]} ] ] }
e6bb88b0 55
5f654d8e 56sub length {
57 my ($array) = @_;
58 CORE::scalar @$array;
59}
60
61sub grep {
62 my ($array, $sub) = @_;
63 [ CORE::grep { $sub->($_) } @$array ];
64}
65
66sub map {
67 my ($array, $sub) = @_;
68 [ CORE::map { $sub->($_) } @$array ];
69}
70
71sub join {
feffe00c 72 my ($array, $sep) = @_;
73 $sep ||= '';
5f654d8e 74 CORE::join $sep, @$array;
75}
76
77sub reverse {
78 my ($array) = @_;
e6bb88b0 79 [ CORE::reverse @$array ];
5f654d8e 80}
81
82sub sort {
83 my ($array, $sub) = @_;
84 $sub ||= sub { $a cmp $b };
85 [ CORE::sort { $sub->($a, $b) } @$array ];
5dc78481 86}
87
260cc81f 88## ::Indexed implementation
5dc78481 89
260cc81f 90sub at {
91 my ($array, $index) = @_;
92 $array->[$index];
93}
5dc78481 94
260cc81f 95sub put {
96 my ($array, $index, $value) = @_;
97 $array->[$index] = $value;
98}
5dc78481 99
6cf5bcf2 100sub exists {
101 my ($array, $index) = @_;
102 CORE::exists $array->[$index];
103}
5dc78481 104
105sub keys {
106 my ($array) = @_;
107 [ 0 .. $#{$array} ];
108}
109
110sub values {
111 my ($array) = @_;
112 [ @$array ];
113}
114
115sub kv {
116 my ($array) = @_;
feffe00c 117 $array->keys->map(sub { [ $_, $array->[$_] ] });
5dc78481 118}
e6bb88b0 119
2197a7c0 120sub flatten {
121 @{$_[0]}
122}
123
7fc99864 124## Junctions
125
126sub all {
127 my ($array) = @_;
aaf111de 128 return Perl6::Junction::all(@$array);
7fc99864 129}
130
131sub any {
132 my ($array) = @_;
aaf111de 133 return Perl6::Junction::any(@$array);
7fc99864 134}
135
136sub none {
137 my ($array) = @_;
aaf111de 138 return Perl6::Junction::none(@$array);
7fc99864 139}
140
141sub one {
142 my ($array) = @_;
aaf111de 143 return Perl6::Junction::one(@$array);
7fc99864 144}
145
3f4dd8b7 146## Print
147
148sub print { CORE::print @{$_[0]} }
149sub say { CORE::print @{$_[0]}, "\n" }
150
5f654d8e 1511;
31d40d73 152
153__END__
154
155=pod
156
157=head1 NAME
158
159Moose::Autobox::Array - the Array role
160
161=head1 SYNOPOSIS
162
163 use Moose::Autobox;
31d40d73 164
5272f13f 165 [ 1..5 ]->isa('ARRAY'); # true
166 [ a..z ]->does('Moose::Autobox::Array'); # true
167 [ 0..2 ]->does('Moose::Autobox::List'); # true
168
31d40d73 169 print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
5272f13f 170
171 print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
f6e003cc 172
173 print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true'
31d40d73 174
175=head1 DESCRIPTION
176
8937074a 177This is a role to describe operations on the Array type.
178
260cc81f 179=head1 METHODS
180
181=over 4
182
260cc81f 183=item B<pop>
184
5272f13f 185=item B<push ($value)>
260cc81f 186
187=item B<shift>
188
5272f13f 189=item B<unshift ($value)>
260cc81f 190
5272f13f 191=item B<delete ($index)>
260cc81f 192
5272f13f 193=item B<sprintf ($format_string)>
260cc81f 194
c11e6a74 195=item B<slice (@indices)>
196
2197a7c0 197=item B<flatten>
198
260cc81f 199=back
200
5272f13f 201=head2 Indexed implementation
260cc81f 202
203=over 4
204
5272f13f 205=item B<at ($index)>
260cc81f 206
5272f13f 207=item B<put ($index, $value)>
260cc81f 208
5272f13f 209=item B<exists ($index)>
260cc81f 210
211=item B<keys>
212
260cc81f 213=item B<values>
214
5272f13f 215=item B<kv>
216
260cc81f 217=back
218
5272f13f 219=head2 List implementation
260cc81f 220
221=over 4
222
223=item B<head>
224
225=item B<tail>
226
5272f13f 227=item B<join (?$seperator)>
260cc81f 228
229=item B<length>
230
5272f13f 231=item B<map (\&block)>
260cc81f 232
5272f13f 233=item B<grep (\&block)>
260cc81f 234
09a0196c 235Note that, in both the above, $_ is in scope within the code block, as well as
236being passed as $_[0]. As per CORE::map and CORE::grep, $_ is an alias to
237the list value, so can be used to to modify the list, viz:
238
239 use Moose::Autobox;
240
241 my $foo = [1, 2, 3];
242 $foo->map( sub {$_++} );
243 print $foo->dump;
244
245yields
246
247 $VAR1 = [
248 2,
249 3,
250 4
251 ];
252
260cc81f 253=item B<reverse>
254
5272f13f 255=item B<sort (?\&block)>
256
257=back
258
7fc99864 259=head2 Junctions
260
261=over 4
262
263=item B<all>
264
265=item B<any>
266
267=item B<none>
268
269=item B<one>
270
271=back
272
5272f13f 273=over 4
274
275=item B<meta>
260cc81f 276
3f4dd8b7 277=item B<print>
278
279=item B<say>
280
260cc81f 281=back
282
31d40d73 283=head1 BUGS
284
285All complex software has bugs lurking in it, and this module is no
286exception. If you find a bug please either email me, or add the bug
287to cpan-RT.
288
289=head1 AUTHOR
290
291Stevan Little E<lt>stevan@iinteractive.comE<gt>
292
293=head1 COPYRIGHT AND LICENSE
294
ea4e64bf 295Copyright 2006-2008 by Infinity Interactive, Inc.
31d40d73 296
297L<http://www.iinteractive.com>
298
299This library is free software; you can redistribute it and/or modify
300it under the same terms as Perl itself.
301
f6e003cc 302=cut