Commit | Line | Data |
5f654d8e |
1 | package Moose::Autobox::Array; |
2 | use Moose::Role 'with'; |
7fc99864 |
3 | use Perl6::Junction; |
7dad2765 |
4 | use Moose::Autobox; |
5f654d8e |
5 | |
e3598a18 |
6 | our $VERSION = '0.10'; |
5f654d8e |
7 | |
e6bb88b0 |
8 | with 'Moose::Autobox::Ref', |
31d40d73 |
9 | 'Moose::Autobox::List', |
10 | 'Moose::Autobox::Indexed'; |
6cf5bcf2 |
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 | } |
5f654d8e |
30 | |
6cf5bcf2 |
31 | sub delete { |
32 | my ($array, $index) = @_; |
33 | CORE::delete $array->[$index]; |
34 | } |
35 | |
36 | sub shift { |
37 | my ($array) = @_; |
38 | CORE::shift @$array; |
c11e6a74 |
39 | } |
40 | |
41 | sub 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 |
49 | sub sprintf { CORE::sprintf $_[1], @{$_[0]} } |
50 | |
51 | ## ::List interface implementation |
52 | |
53 | sub head { $_[0]->[0] } |
54 | sub tail { [ @{$_[0]}[ 1 .. $#{$_[0]} ] ] } |
e6bb88b0 |
55 | |
5f654d8e |
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 { |
feffe00c |
72 | my ($array, $sep) = @_; |
73 | $sep ||= ''; |
5f654d8e |
74 | CORE::join $sep, @$array; |
75 | } |
76 | |
77 | sub reverse { |
78 | my ($array) = @_; |
e6bb88b0 |
79 | [ CORE::reverse @$array ]; |
5f654d8e |
80 | } |
81 | |
82 | sub sort { |
83 | my ($array, $sub) = @_; |
84 | $sub ||= sub { $a cmp $b }; |
85 | [ CORE::sort { $sub->($a, $b) } @$array ]; |
5dc78481 |
86 | } |
87 | |
cc6691de |
88 | sub first { |
89 | $_[0]->[0]; |
90 | } |
91 | |
92 | sub last { |
93 | $_[0]->[$#{$_[0]}]; |
94 | } |
95 | |
260cc81f |
96 | ## ::Indexed implementation |
5dc78481 |
97 | |
260cc81f |
98 | sub at { |
99 | my ($array, $index) = @_; |
100 | $array->[$index]; |
101 | } |
5dc78481 |
102 | |
260cc81f |
103 | sub put { |
104 | my ($array, $index, $value) = @_; |
105 | $array->[$index] = $value; |
106 | } |
5dc78481 |
107 | |
6cf5bcf2 |
108 | sub exists { |
109 | my ($array, $index) = @_; |
110 | CORE::exists $array->[$index]; |
111 | } |
5dc78481 |
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) = @_; |
feffe00c |
125 | $array->keys->map(sub { [ $_, $array->[$_] ] }); |
5dc78481 |
126 | } |
e6bb88b0 |
127 | |
2dcdd7d7 |
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) = @_; |
2224d5b4 |
137 | $sub->($_) for (0 .. $#$array); |
2dcdd7d7 |
138 | } |
139 | |
140 | sub each_value { |
141 | my ($array, $sub) = @_; |
142 | $sub->($_) for @$array; |
143 | } |
144 | |
145 | # end indexed |
146 | |
2197a7c0 |
147 | sub flatten { |
148 | @{$_[0]} |
149 | } |
150 | |
e477b088 |
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 | |
7fc99864 |
169 | ## Junctions |
170 | |
171 | sub all { |
172 | my ($array) = @_; |
aaf111de |
173 | return Perl6::Junction::all(@$array); |
7fc99864 |
174 | } |
175 | |
176 | sub any { |
177 | my ($array) = @_; |
aaf111de |
178 | return Perl6::Junction::any(@$array); |
7fc99864 |
179 | } |
180 | |
181 | sub none { |
182 | my ($array) = @_; |
aaf111de |
183 | return Perl6::Junction::none(@$array); |
7fc99864 |
184 | } |
185 | |
186 | sub one { |
187 | my ($array) = @_; |
aaf111de |
188 | return Perl6::Junction::one(@$array); |
7fc99864 |
189 | } |
190 | |
3f4dd8b7 |
191 | ## Print |
192 | |
193 | sub print { CORE::print @{$_[0]} } |
194 | sub say { CORE::print @{$_[0]}, "\n" } |
195 | |
5f654d8e |
196 | 1; |
31d40d73 |
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; |
31d40d73 |
209 | |
5272f13f |
210 | [ 1..5 ]->isa('ARRAY'); # true |
211 | [ a..z ]->does('Moose::Autobox::Array'); # true |
212 | [ 0..2 ]->does('Moose::Autobox::List'); # true |
213 | |
31d40d73 |
214 | print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', '); |
5272f13f |
215 | |
216 | print [ 1, 'number' ]->sprintf('%d is the loneliest %s'); |
f6e003cc |
217 | |
218 | print ([ 1 .. 5 ]->any == 3) ? 'true' : 'false'; # prints 'true' |
31d40d73 |
219 | |
220 | =head1 DESCRIPTION |
221 | |
8937074a |
222 | This is a role to describe operations on the Array type. |
223 | |
260cc81f |
224 | =head1 METHODS |
225 | |
226 | =over 4 |
227 | |
260cc81f |
228 | =item B<pop> |
229 | |
5272f13f |
230 | =item B<push ($value)> |
260cc81f |
231 | |
232 | =item B<shift> |
233 | |
5272f13f |
234 | =item B<unshift ($value)> |
260cc81f |
235 | |
5272f13f |
236 | =item B<delete ($index)> |
260cc81f |
237 | |
5272f13f |
238 | =item B<sprintf ($format_string)> |
260cc81f |
239 | |
c11e6a74 |
240 | =item B<slice (@indices)> |
241 | |
2197a7c0 |
242 | =item B<flatten> |
243 | |
e477b088 |
244 | =item B<flatten_deep ($depth)> |
245 | |
cc6691de |
246 | =item B<first> |
247 | |
248 | =item B<last> |
249 | |
260cc81f |
250 | =back |
251 | |
5272f13f |
252 | =head2 Indexed implementation |
260cc81f |
253 | |
254 | =over 4 |
255 | |
5272f13f |
256 | =item B<at ($index)> |
260cc81f |
257 | |
5272f13f |
258 | =item B<put ($index, $value)> |
260cc81f |
259 | |
5272f13f |
260 | =item B<exists ($index)> |
260cc81f |
261 | |
262 | =item B<keys> |
263 | |
260cc81f |
264 | =item B<values> |
265 | |
5272f13f |
266 | =item B<kv> |
267 | |
c0ab09e3 |
268 | =item B<each> |
269 | |
270 | =item B<each_key> |
271 | |
272 | =item B<each_value> |
273 | |
260cc81f |
274 | =back |
275 | |
5272f13f |
276 | =head2 List implementation |
260cc81f |
277 | |
278 | =over 4 |
279 | |
280 | =item B<head> |
281 | |
282 | =item B<tail> |
283 | |
5272f13f |
284 | =item B<join (?$seperator)> |
260cc81f |
285 | |
286 | =item B<length> |
287 | |
5272f13f |
288 | =item B<map (\&block)> |
260cc81f |
289 | |
5272f13f |
290 | =item B<grep (\&block)> |
260cc81f |
291 | |
09a0196c |
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 | |
260cc81f |
310 | =item B<reverse> |
311 | |
5272f13f |
312 | =item B<sort (?\&block)> |
313 | |
314 | =back |
315 | |
7fc99864 |
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 | |
5272f13f |
330 | =over 4 |
331 | |
332 | =item B<meta> |
260cc81f |
333 | |
3f4dd8b7 |
334 | =item B<print> |
335 | |
336 | =item B<say> |
337 | |
260cc81f |
338 | =back |
339 | |
31d40d73 |
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 | |
ea4e64bf |
352 | Copyright 2006-2008 by Infinity Interactive, Inc. |
31d40d73 |
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 | |
f6e003cc |
359 | =cut |