use parent, not base
[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
f3e571b2 6our $VERSION = '0.12';
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
8b14b072 88sub first {
89 $_[0]->[0];
90}
91
92sub last {
93 $_[0]->[$#{$_[0]}];
94}
95
260cc81f 96## ::Indexed implementation
5dc78481 97
260cc81f 98sub at {
99 my ($array, $index) = @_;
100 $array->[$index];
101}
5dc78481 102
260cc81f 103sub put {
104 my ($array, $index, $value) = @_;
105 $array->[$index] = $value;
106}
5dc78481 107
6cf5bcf2 108sub exists {
109 my ($array, $index) = @_;
110 CORE::exists $array->[$index];
111}
5dc78481 112
113sub keys {
114 my ($array) = @_;
115 [ 0 .. $#{$array} ];
116}
117
118sub values {
119 my ($array) = @_;
120 [ @$array ];
121}
122
123sub kv {
124 my ($array) = @_;
feffe00c 125 $array->keys->map(sub { [ $_, $array->[$_] ] });
5dc78481 126}
e6bb88b0 127
2dcdd7d7 128sub each {
129 my ($array, $sub) = @_;
130 for my $i (0 .. $#$array) {
131 $sub->($i, $array->[ $i ]);
132 }
133}
134
135sub each_key {
136 my ($array, $sub) = @_;
2224d5b4 137 $sub->($_) for (0 .. $#$array);
2dcdd7d7 138}
139
140sub each_value {
141 my ($array, $sub) = @_;
142 $sub->($_) for @$array;
143}
144
450776ec 145sub each_n_values {
0e480911 146 my ($array, $n, $sub) = @_;
147 my $it = List::MoreUtils::natatime($n, @$array);
148
149 while (my @vals = $it->()) {
150 $sub->(@vals);
151 }
152
153 return;
154}
155
2dcdd7d7 156# end indexed
157
2197a7c0 158sub flatten {
159 @{$_[0]}
160}
161
e477b088 162sub _flatten_deep {
163 my @array = @_;
164 my $depth = CORE::pop @array;
165 --$depth if (defined($depth));
166
167 CORE::map {
168 (ref eq 'ARRAY')
169 ? (defined($depth) && $depth == -1) ? $_ : _flatten_deep( @$_, $depth )
170 : $_
171 } @array;
172
173}
174
175sub flatten_deep {
176 my ($array, $depth) = @_;
177 [ _flatten_deep(@$array, $depth) ];
178}
179
7fc99864 180## Junctions
181
182sub all {
183 my ($array) = @_;
aaf111de 184 return Perl6::Junction::all(@$array);
7fc99864 185}
186
187sub any {
188 my ($array) = @_;
aaf111de 189 return Perl6::Junction::any(@$array);
7fc99864 190}
191
192sub none {
193 my ($array) = @_;
aaf111de 194 return Perl6::Junction::none(@$array);
7fc99864 195}
196
197sub one {
198 my ($array) = @_;
aaf111de 199 return Perl6::Junction::one(@$array);
7fc99864 200}
201
3f4dd8b7 202## Print
203
204sub print { CORE::print @{$_[0]} }
205sub say { CORE::print @{$_[0]}, "\n" }
206
5f654d8e 2071;
31d40d73 208
209__END__
210
211=pod
212
213=head1 NAME
214
215Moose::Autobox::Array - the Array role
216
217=head1 SYNOPOSIS
218
219 use Moose::Autobox;
31d40d73 220
5272f13f 221 [ 1..5 ]->isa('ARRAY'); # true
222 [ a..z ]->does('Moose::Autobox::Array'); # true
223 [ 0..2 ]->does('Moose::Autobox::List'); # true
224
31d40d73 225 print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
5272f13f 226
227 print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
f6e003cc 228
229 print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true'
31d40d73 230
231=head1 DESCRIPTION
232
8937074a 233This is a role to describe operations on the Array type.
234
260cc81f 235=head1 METHODS
236
237=over 4
238
260cc81f 239=item B<pop>
240
5272f13f 241=item B<push ($value)>
260cc81f 242
243=item B<shift>
244
5272f13f 245=item B<unshift ($value)>
260cc81f 246
5272f13f 247=item B<delete ($index)>
260cc81f 248
5272f13f 249=item B<sprintf ($format_string)>
260cc81f 250
c11e6a74 251=item B<slice (@indices)>
252
2197a7c0 253=item B<flatten>
254
e477b088 255=item B<flatten_deep ($depth)>
256
8b14b072 257=item B<first>
258
259=item B<last>
0e480911 260
260cc81f 261=back
262
5272f13f 263=head2 Indexed implementation
260cc81f 264
265=over 4
266
5272f13f 267=item B<at ($index)>
260cc81f 268
5272f13f 269=item B<put ($index, $value)>
260cc81f 270
5272f13f 271=item B<exists ($index)>
260cc81f 272
273=item B<keys>
274
260cc81f 275=item B<values>
276
5272f13f 277=item B<kv>
278
c0ab09e3 279=item B<each>
280
281=item B<each_key>
282
283=item B<each_value>
284
8b14b072 285=item B<each_n_values ($n, $callback)>
286
260cc81f 287=back
288
5272f13f 289=head2 List implementation
260cc81f 290
291=over 4
292
293=item B<head>
294
295=item B<tail>
296
5272f13f 297=item B<join (?$seperator)>
260cc81f 298
299=item B<length>
300
5272f13f 301=item B<map (\&block)>
260cc81f 302
5272f13f 303=item B<grep (\&block)>
260cc81f 304
09a0196c 305Note that, in both the above, $_ is in scope within the code block, as well as
306being passed as $_[0]. As per CORE::map and CORE::grep, $_ is an alias to
307the list value, so can be used to to modify the list, viz:
308
309 use Moose::Autobox;
310
311 my $foo = [1, 2, 3];
312 $foo->map( sub {$_++} );
313 print $foo->dump;
314
315yields
316
317 $VAR1 = [
318 2,
319 3,
320 4
321 ];
322
260cc81f 323=item B<reverse>
324
5272f13f 325=item B<sort (?\&block)>
326
327=back
328
7fc99864 329=head2 Junctions
330
331=over 4
332
333=item B<all>
334
335=item B<any>
336
337=item B<none>
338
339=item B<one>
340
341=back
342
5272f13f 343=over 4
344
345=item B<meta>
260cc81f 346
3f4dd8b7 347=item B<print>
348
349=item B<say>
350
260cc81f 351=back
352
31d40d73 353=head1 BUGS
354
355All complex software has bugs lurking in it, and this module is no
356exception. If you find a bug please either email me, or add the bug
357to cpan-RT.
358
359=head1 AUTHOR
360
361Stevan Little E<lt>stevan@iinteractive.comE<gt>
362
363=head1 COPYRIGHT AND LICENSE
364
ea4e64bf 365Copyright 2006-2008 by Infinity Interactive, Inc.
31d40d73 366
367L<http://www.iinteractive.com>
368
369This library is free software; you can redistribute it and/or modify
370it under the same terms as Perl itself.
371
f6e003cc 372=cut