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