6d4c1e8bd3eaba25ef8f48271df4552d7d45d9d9
[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 at {
51     my ($array, $index) = @_;
52     $array->[$index];
53
54
55 sub put {
56     my ($array, $index, $value) = @_;
57     $array->[$index] = $value;
58 }
59  
60 sub length {
61     my ($array) = @_;
62     CORE::scalar @$array;
63 }
64
65 sub grep { 
66     my ($array, $sub) = @_; 
67     [ CORE::grep { $sub->($_) } @$array ]; 
68 }
69
70 sub map { 
71     my ($array, $sub) = @_; 
72     [ CORE::map { $sub->($_) } @$array ]; 
73 }
74
75 sub join { 
76     my ($array, $sep) = @_;    
77     $sep ||= ''; 
78     CORE::join $sep, @$array; 
79 }
80
81 sub reverse { 
82     my ($array) = @_;
83     [ CORE::reverse @$array ];
84 }
85
86 sub sort { 
87     my ($array, $sub) = @_;     
88     $sub ||= sub { $a cmp $b }; 
89     [ CORE::sort { $sub->($a, $b) } @$array ]; 
90 }    
91
92 # ::Value requirement
93
94 sub print { CORE::print @{$_[0]} }
95
96 ## ::Indexed implementation
97
98 sub exists {
99     my ($array, $index) = @_;    
100     CORE::exists $array->[$index];    
101 }
102
103 sub keys { 
104     my ($array) = @_;    
105     [ 0 .. $#{$array} ];
106 }
107
108 sub values { 
109     my ($array) = @_;    
110     [ @$array ];
111 }
112
113 sub kv {
114     my ($array) = @_;   
115     $array->keys->map(sub { [ $_, $array->[$_] ] });
116 }
117
118 1;
119
120 __END__
121
122 =pod
123
124 =head1 NAME 
125
126 Moose::Autobox::Array - the Array role
127
128 =head1 SYNOPOSIS
129
130   use Moose::Autobox;
131   use autobox;
132     
133   print "Squares: " . [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
134
135 =head1 DESCRIPTION
136
137 =head1 BUGS
138
139 All complex software has bugs lurking in it, and this module is no 
140 exception. If you find a bug please either email me, or add the bug
141 to cpan-RT.
142
143 =head1 AUTHOR
144
145 Stevan Little E<lt>stevan@iinteractive.comE<gt>
146
147 =head1 COPYRIGHT AND LICENSE
148
149 Copyright 2006 by Infinity Interactive, Inc.
150
151 L<http://www.iinteractive.com>
152
153 This library is free software; you can redistribute it and/or modify
154 it under the same terms as Perl itself.
155
156 =cut