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 | |
8b14b072 |
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 | |
450776ec |
145 | sub 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 |
158 | sub flatten { |
159 | @{$_[0]} |
160 | } |
161 | |
e477b088 |
162 | sub _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 | |
175 | sub flatten_deep { |
176 | my ($array, $depth) = @_; |
177 | [ _flatten_deep(@$array, $depth) ]; |
178 | } |
179 | |
7fc99864 |
180 | ## Junctions |
181 | |
182 | sub all { |
183 | my ($array) = @_; |
aaf111de |
184 | return Perl6::Junction::all(@$array); |
7fc99864 |
185 | } |
186 | |
187 | sub any { |
188 | my ($array) = @_; |
aaf111de |
189 | return Perl6::Junction::any(@$array); |
7fc99864 |
190 | } |
191 | |
192 | sub none { |
193 | my ($array) = @_; |
aaf111de |
194 | return Perl6::Junction::none(@$array); |
7fc99864 |
195 | } |
196 | |
197 | sub one { |
198 | my ($array) = @_; |
aaf111de |
199 | return Perl6::Junction::one(@$array); |
7fc99864 |
200 | } |
201 | |
3f4dd8b7 |
202 | ## Print |
203 | |
204 | sub print { CORE::print @{$_[0]} } |
205 | sub say { CORE::print @{$_[0]}, "\n" } |
206 | |
5f654d8e |
207 | 1; |
31d40d73 |
208 | |
209 | __END__ |
210 | |
211 | =pod |
212 | |
213 | =head1 NAME |
214 | |
215 | Moose::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 |
233 | This 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 |
305 | Note that, in both the above, $_ is in scope within the code block, as well as |
306 | being passed as $_[0]. As per CORE::map and CORE::grep, $_ is an alias to |
307 | the 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 | |
315 | yields |
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 | |
355 | All complex software has bugs lurking in it, and this module is no |
356 | exception. If you find a bug please either email me, or add the bug |
357 | to cpan-RT. |
358 | |
359 | =head1 AUTHOR |
360 | |
361 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
362 | |
363 | =head1 COPYRIGHT AND LICENSE |
364 | |
ea4e64bf |
365 | Copyright 2006-2008 by Infinity Interactive, Inc. |
31d40d73 |
366 | |
367 | L<http://www.iinteractive.com> |
368 | |
369 | This library is free software; you can redistribute it and/or modify |
370 | it under the same terms as Perl itself. |
371 | |
f6e003cc |
372 | =cut |