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