ecc41276027319e6b2a4e908b0e270fa3b40bde4
[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.03';
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 flatten {
121     @{$_[0]}
122 }
123
124 ## Junctions
125
126 sub all {
127     my ($array) = @_;     
128     return Perl6::Junction::All->new(@$array);
129 }
130
131 sub any {
132     my ($array) = @_;     
133     return Perl6::Junction::Any->new(@$array);
134 }
135
136 sub none {
137     my ($array) = @_;     
138     return Perl6::Junction::None->new(@$array);
139 }
140
141 sub one {
142     my ($array) = @_; 
143     return Perl6::Junction::One->new(@$array);
144 }
145
146 ## Print
147
148 sub print { CORE::print @{$_[0]} }
149 sub say   { CORE::print @{$_[0]}, "\n" }
150
151 1;
152
153 __END__
154
155 =pod
156
157 =head1 NAME 
158
159 Moose::Autobox::Array - the Array role
160
161 =head1 SYNOPOSIS
162
163   use Moose::Autobox;
164     
165   [ 1..5 ]->isa('ARRAY'); # true
166   [ a..z ]->does('Moose::Autobox::Array'); # true
167   [ 0..2 ]->does('Moose::Autobox::List'); # true  
168     
169   print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
170   
171   print [ 1, 'number' ]->sprintf('%d is the loneliest %s');
172   
173   print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true'
174
175 =head1 DESCRIPTION
176
177 This is a role to describe operations on the Array type. 
178
179 =head1 METHODS
180
181 =over 4
182
183 =item B<pop>
184
185 =item B<push ($value)>
186
187 =item B<shift>
188
189 =item B<unshift ($value)>
190
191 =item B<delete ($index)>
192
193 =item B<sprintf ($format_string)>
194
195 =item B<slice (@indices)>
196
197 =item B<flatten>
198
199 =back
200
201 =head2 Indexed implementation
202
203 =over 4
204
205 =item B<at ($index)>
206
207 =item B<put ($index, $value)>
208
209 =item B<exists ($index)>
210
211 =item B<keys>
212
213 =item B<values>
214
215 =item B<kv>
216
217 =back
218
219 =head2 List implementation
220
221 =over 4
222
223 =item B<head>
224
225 =item B<tail>
226
227 =item B<join (?$seperator)>
228
229 =item B<length>
230
231 =item B<map (\&block)>
232
233 =item B<grep (\&block)>
234
235 =item B<reverse>
236
237 =item B<sort (?\&block)>
238
239 =back
240
241 =head2 Junctions
242
243 =over 4
244
245 =item B<all>
246
247 =item B<any>
248
249 =item B<none>
250
251 =item B<one>
252
253 =back
254
255 =over 4
256
257 =item B<meta>
258
259 =item B<print>
260
261 =item B<say>
262
263 =back
264
265 =head1 BUGS
266
267 All complex software has bugs lurking in it, and this module is no 
268 exception. If you find a bug please either email me, or add the bug
269 to cpan-RT.
270
271 =head1 AUTHOR
272
273 Stevan Little E<lt>stevan@iinteractive.comE<gt>
274
275 =head1 COPYRIGHT AND LICENSE
276
277 Copyright 2006-2008 by Infinity Interactive, Inc.
278
279 L<http://www.iinteractive.com>
280
281 This library is free software; you can redistribute it and/or modify
282 it under the same terms as Perl itself.
283
284 =cut