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