foo
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Array.pm
CommitLineData
5f654d8e 1package Moose::Autobox::Array;
2use Moose::Role 'with';
252ab1a2 3use autobox;
5f654d8e 4
5our $VERSION = '0.01';
6
e6bb88b0 7with 'Moose::Autobox::Ref',
31d40d73 8 'Moose::Autobox::List',
9 'Moose::Autobox::Indexed';
6cf5bcf2 10
11## Array Interface
12
13sub pop {
14 my ($array) = @_;
15 CORE::pop @$array;
16}
17
18sub push {
19 my ($array, @rest) = @_;
20 CORE::push @$array, @rest;
21 $array;
22}
23
24sub unshift {
25 my ($array, @rest) = @_;
26 CORE::unshift @$array, @rest;
27 $array;
28}
5f654d8e 29
6cf5bcf2 30sub delete {
31 my ($array, $index) = @_;
32 CORE::delete $array->[$index];
33}
34
35sub 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
43sub sprintf { CORE::sprintf $_[1], @{$_[0]} }
44
45## ::List interface implementation
46
47sub head { $_[0]->[0] }
48sub tail { [ @{$_[0]}[ 1 .. $#{$_[0]} ] ] }
e6bb88b0 49
31d40d73 50sub at {
51 my ($array, $index) = @_;
52 $array->[$index];
53}
54
55sub put {
56 my ($array, $index, $value) = @_;
57 $array->[$index] = $value;
58}
59
5f654d8e 60sub length {
61 my ($array) = @_;
62 CORE::scalar @$array;
63}
64
65sub grep {
66 my ($array, $sub) = @_;
67 [ CORE::grep { $sub->($_) } @$array ];
68}
69
70sub map {
71 my ($array, $sub) = @_;
72 [ CORE::map { $sub->($_) } @$array ];
73}
74
75sub join {
feffe00c 76 my ($array, $sep) = @_;
77 $sep ||= '';
5f654d8e 78 CORE::join $sep, @$array;
79}
80
81sub reverse {
82 my ($array) = @_;
e6bb88b0 83 [ CORE::reverse @$array ];
5f654d8e 84}
85
86sub sort {
87 my ($array, $sub) = @_;
88 $sub ||= sub { $a cmp $b };
89 [ CORE::sort { $sub->($a, $b) } @$array ];
5dc78481 90}
91
6cf5bcf2 92# ::Value requirement
5dc78481 93
6cf5bcf2 94sub print { CORE::print @{$_[0]} }
5dc78481 95
6cf5bcf2 96## ::Indexed implementation
5dc78481 97
6cf5bcf2 98sub exists {
99 my ($array, $index) = @_;
100 CORE::exists $array->[$index];
101}
5dc78481 102
103sub keys {
104 my ($array) = @_;
105 [ 0 .. $#{$array} ];
106}
107
108sub values {
109 my ($array) = @_;
110 [ @$array ];
111}
112
113sub kv {
114 my ($array) = @_;
feffe00c 115 $array->keys->map(sub { [ $_, $array->[$_] ] });
5dc78481 116}
e6bb88b0 117
5f654d8e 1181;
31d40d73 119
120__END__
121
122=pod
123
124=head1 NAME
125
126Moose::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
139All complex software has bugs lurking in it, and this module is no
140exception. If you find a bug please either email me, or add the bug
141to cpan-RT.
142
143=head1 AUTHOR
144
145Stevan Little E<lt>stevan@iinteractive.comE<gt>
146
147=head1 COPYRIGHT AND LICENSE
148
149Copyright 2006 by Infinity Interactive, Inc.
150
151L<http://www.iinteractive.com>
152
153This library is free software; you can redistribute it and/or modify
154it under the same terms as Perl itself.
155
156=cut