38e94f1aa5236373df6ed8a0e31de6fc5ce097b3
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Array.pm
1 package Moose::Autobox::Array;
2 use Moose::Role 'with';
3 use autobox;
4
5 our $VERSION = '0.01';
6
7 with 'Moose::Autobox::Ref',
8      'Moose::Autobox::List',
9      'Moose::Autobox::Indexed';
10     
11 ## Array Interface
12
13 sub pop { 
14     my ($array) = @_;    
15     CORE::pop @$array; 
16 }
17
18 sub push { 
19     my ($array, @rest) = @_;
20     CORE::push @$array, @rest;  
21     $array; 
22 }
23
24 sub unshift { 
25     my ($array, @rest) = @_;    
26     CORE::unshift @$array, @rest; 
27     $array; 
28 }
29
30 sub delete { 
31     my ($array, $index) = @_;    
32     CORE::delete $array->[$index];
33 }
34
35 sub shift { 
36     my ($array) = @_;    
37     CORE::shift @$array; 
38 }     
39
40 # NOTE: 
41 # sprintf args need to be reversed, 
42 # because the invocant is the array
43 sub sprintf { CORE::sprintf $_[1], @{$_[0]} }
44
45 ## ::List interface implementation
46
47 sub head { $_[0]->[0] }
48 sub tail { [ @{$_[0]}[ 1 .. $#{$_[0]} ] ] }
49  
50 sub length {
51     my ($array) = @_;
52     CORE::scalar @$array;
53 }
54
55 sub grep { 
56     my ($array, $sub) = @_; 
57     [ CORE::grep { $sub->($_) } @$array ]; 
58 }
59
60 sub map { 
61     my ($array, $sub) = @_; 
62     [ CORE::map { $sub->($_) } @$array ]; 
63 }
64
65 sub join { 
66     my ($array, $sep) = @_;    
67     $sep ||= ''; 
68     CORE::join $sep, @$array; 
69 }
70
71 sub reverse { 
72     my ($array) = @_;
73     [ CORE::reverse @$array ];
74 }
75
76 sub sort { 
77     my ($array, $sub) = @_;     
78     $sub ||= sub { $a cmp $b }; 
79     [ CORE::sort { $sub->($a, $b) } @$array ]; 
80 }    
81
82 ## ::Indexed implementation
83
84 sub at {
85     my ($array, $index) = @_;
86     $array->[$index];
87
88
89 sub put {
90     my ($array, $index, $value) = @_;
91     $array->[$index] = $value;
92 }
93
94 sub exists {
95     my ($array, $index) = @_;    
96     CORE::exists $array->[$index];    
97 }
98
99 sub keys { 
100     my ($array) = @_;    
101     [ 0 .. $#{$array} ];
102 }
103
104 sub values { 
105     my ($array) = @_;    
106     [ @$array ];
107 }
108
109 sub kv {
110     my ($array) = @_;   
111     $array->keys->map(sub { [ $_, $array->[$_] ] });
112 }
113
114 1;
115
116 __END__
117
118 =pod
119
120 =head1 NAME 
121
122 Moose::Autobox::Array - the Array role
123
124 =head1 SYNOPOSIS
125
126   use Moose::Autobox;
127   use autobox;
128     
129   print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
130
131 =head1 DESCRIPTION
132
133 This is a role to describe operations on the Array type. 
134
135 =head1 METHODS
136
137 =over 4
138
139 =item B<meta>
140
141 =item B<pop>
142
143 =item B<push>
144
145 =item B<shift>
146
147 =item B<unshift>
148
149 =item B<delete>
150
151 =item B<sprintf>
152
153 =back
154
155 =head2 Moose::Autobox::Indexed implementation
156
157 =over 4
158
159 =item B<at>
160
161 =item B<put>
162
163 =item B<exists>
164
165 =item B<keys>
166
167 =item B<kv>
168
169 =item B<values>
170
171 =back
172
173 =head2 Moose::Autobox::List implementation
174
175 =over 4
176
177 =item B<head>
178
179 =item B<tail>
180
181 =item B<join>
182
183 =item B<length>
184
185 =item B<map>
186
187 =item B<grep>
188
189 =item B<reverse>
190
191 =item B<sort>
192
193 =back
194
195 =head1 BUGS
196
197 All complex software has bugs lurking in it, and this module is no 
198 exception. If you find a bug please either email me, or add the bug
199 to cpan-RT.
200
201 =head1 AUTHOR
202
203 Stevan Little E<lt>stevan@iinteractive.comE<gt>
204
205 =head1 COPYRIGHT AND LICENSE
206
207 Copyright 2006 by Infinity Interactive, Inc.
208
209 L<http://www.iinteractive.com>
210
211 This library is free software; you can redistribute it and/or modify
212 it under the same terms as Perl itself.
213
214 =cut