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