MoCo/List.pm

filter: Statementstmt 38.2% 68 / 178 Branchbran 27.1% 19 / 70 Conditioncond 20.0% 4 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 38.1% 21 / 55 Podpod 88.8% 32 / 36 totaltotal 38.8% 144 / 371 CC 51 SCAR 25.5
CC
51
Cov
34%
slice
72.0
delete
30.0
delete_at
20.0
CRAP 12.9
« MoCo/Column/utf8.pm ↑ Summary MoCo/DataBase.pm »
1 package DBIx::MoCo::List;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use List::MoreUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 our $AUTOLOAD;
9  
10 39 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=16.5
11 39 my $self = $_[0];
12 39 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
3900
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
13 39 ▶ $self = undef unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 39 39
14 39 (my $method = $AUTOLOAD) =~ s!.+::!!;
15 39 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 37 2 39
16 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ if ($method =~ /^map_(.+)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^map_(.+)$/o) 2 0 2
18 2 *$AUTOLOAD = $class->_map_handler($1);
19 2 goto &$AUTOLOAD;
20 }
21 }
22  
23 2 ▶sub _map_handler {
Subroutine _map_handler: called CC=1 SCAR=0.0
24 2 my $class = shift;
25 2 my $method = shift;
26 2 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
27 2 ▶ shift->map(sub { $_->$method() });
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 };
29 }
30  
31 37 ▶sub new {
Subroutine new: called CC=4 SCAR=14.5
Pod undocumented
32 37 my ($class, $array) = @_;
33 37 ▶ $class = ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
2350
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
34 37 ▶ $array ||= [];
Condition50%1/2
$array ||= []
l!l
370
MC/DC0%0/1
$array ||= []
$array
Truth table50%1/2
A
A= $array
Aresult
11
00
35 37 ▶ croak sprintf("Argument must be an array reference (%s)", ref $array)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $array eq "ARRAY" 0 37 37
36 unless ref $array eq 'ARRAY';
37 37 bless $array, $class;
38 }
39  
40 0 ▶sub push {
Subroutine push: not called CC=1 SCAR=6.9
Pod documented
41 0 my $self = shift;
42 0 push @$self, @_;
43 0 $self;
44 }
45  
46 0 ▶sub unshift {
Subroutine unshift: not called CC=1 SCAR=6.9
Pod documented
47 0 my $self = shift;
48 0 unshift @$self, @_;
49 0 $self;
50 }
51  
52 0 ▶sub shift {
Subroutine shift: not called CC=1 SCAR=6.9
Pod documented
53 0 shift @{$_[0]};
54 }
55  
56 1 ▶sub pop {
Subroutine pop: called CC=1 SCAR=0.0
Pod documented
57 1 pop @{$_[0]};
58 }
59  
60 5 ▶sub first {
Subroutine first: called CC=1 SCAR=0.0
Pod documented
61 5 $_[0]->[0];
62 }
63  
64 1 ▶sub last {
Subroutine last: called CC=1 SCAR=0.0
Pod documented
65 1 $_[0]->[-1];
66 }
67  
68 0 ▶sub slice {
Subroutine slice: not called CC=8 SCAR=42.8
Pod documented
69 0 my $self = CORE::shift;
70 0 my ($s, $e) = @_;
71 0 my $last = $#{$self};
72 # warn "s: $s, e: $e, last: $last";
73 0 ▶ if (defined $e) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $e) { } 0 0 0
elsif (defined $s and 0 < $s and $last <= $s) { } 0 0 0
74 0 ▶ if ($s == 0 && $last <= $e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s == 0 and $last <= $e) { } 0 0 0
Condition0%0/3
$s == 0 and $last <= $e
!ll&&!rl&&r
000
MC/DC0%0/2
$s == 0 and $last <= $e
$s == 0 $last <= $e
Truth table0%0/3
A and B
A= $s == 0B= $last <= $e
ABresult
0X0
100
111
75 0 return $self;
76 } else {
77 0 ▶ $e = $last if ($last < $e);
Branch: 1 branch
BranchTrueFalseTotal
if $last < $e 0 0 0
78 0 return $self->new([ @$self[ $s .. $e ] ]);
79 }
80 ▶ } elsif (defined $s && 0 < $s && $last <= $s) {
Condition0%0/6
defined $s and 0 < $s and $last <= $s
!ll&&!rl&&r
000
defined $s and 0 < $s
!ll&&!rl&&r
000
MC/DC0%0/3
defined $s and 0 < $s and $last <= $s
defined $s 0 < $s $last <= $s
Truth table0%0/4
A and B and C
A= defined $sB= 0 < $sC= $last <= $s
ABCresult
0XX0
10X0
1100
1111
81 # warn $self->first . "s: $s, e: $e, self:" . $#{$self};
82 0 return $self->new([]);
83 } else {
84 0 return $self;
85 }
86 }
87  
88 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod documented
89 0 my $self = CORE::shift;
90 0 require Data::Dumper;
91 0 Data::Dumper->new([ $self->to_a ])->Purity(1)->Terse(1)->Dump;
92 }
93  
94 0 ▶sub zip {
Subroutine zip: not called CC=1 SCAR=6.9
Pod documented
95 0 my $self = CORE::shift;
96 0 my $array = \@_;
97 0 my $index = 0;
98 0 ▶ $self->collect(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
99 0 my $ary = $self->new([$_]);
100 0 $ary->push($_->[$index]) for @$array;
101 0 $index++;
102 0 $ary;
103 0 });
104 }
105  
106 0 ▶sub delete {
Subroutine delete: not called CC=5 SCAR=34.0
Pod documented
107 0 my ($self, $value, $code) = @_;
108 0 my $found = 0;
109 0 ▶ do { my $item = $self->shift; $item == $value ? $found = 1 : $self->push($item) } for (0..$self->_last_index);
Branch: 1 branch
BranchTrueFalseTotal
$item == $value ? : 0 0 0
110 $found ? $value
111 0 ▶ : ref $code eq 'CODE' ? do { local $_ = $value; return $code->($_) }
Branch: 2 branches
BranchTrueFalseTotal
$found ? : 0 0 0
ref $code eq 'CODE' ? : 0 0 0
112 : return ;
113 }
114  
115 0 ▶sub delete_at {
Subroutine delete_at: not called CC=4 SCAR=30.0
Pod documented
116 0 my ($self, $pos) = @_;
117 0 my $last_index = $self->_last_index;
118 0 ▶ return if $pos > $last_index ;
Branch: 1 branch
BranchTrueFalseTotal
if $pos > $last_index 0 0 0
119 0 my $result;
120 $_ == $pos ? $result = $self->shift
121 0 ▶ : $self->push($self->shift) for 0..$last_index;
Branch: 1 branch
BranchTrueFalseTotal
$_ == $pos ? : 0 0 0
122 0 return $result;
123 }
124  
125 0 ▶sub delete_if {
Subroutine delete_if: not called CC=4 SCAR=30.0
Pod documented
126 0 my ($self, $code) = @_;
127 0 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
128 0 my $last_index = $self->_last_index;
129 0 for (0..$last_index) {
130 0 my $item = $self->shift;
131 0 local $_ = $item;
132 0 ▶ $self->push($item) if $code->($_);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($_) 0 0 0
133 }
134 0 return $self;
135 }
136  
137 0 ▶sub inject {
Subroutine inject: not called CC=3 SCAR=24.8
Pod documented
138 0 my ($self, $result, $code) = @_;
139 0 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
140 0 $result = $code->($result, $_) for @{$self->dup};
141 0 return $result;
142 }
143  
144 0 ▶sub join {
Subroutine join: not called CC=1 SCAR=6.9
Pod documented
145 0 my ($self, $delimiter) = @_;
146 0 join $delimiter, @$self;
147 }
148  
149 0 ▶sub each_index {
Subroutine each_index: not called CC=1 SCAR=6.9
Pod documented
150 0 my ($self, $code) = @_;
151 0 $self->new([ 0..$self->_last_index ])->each($code);
152 }
153  
154 0 ▶sub _last_index {
Subroutine _last_index: not called CC=2 SCAR=17.9
155 0 my $self = CORE::shift;
156 0 ▶ $self->length ? $self->length - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->length ? : 0 0 0
157 };
158  
159 0 ▶sub concat {
Subroutine concat: not called CC=1 SCAR=6.9
Pod documented
160 0 my ($self, $array) = @_;
161 0 $self->push(@$array);
162 0 $self;
163 }
164  
165 12 *append = \&concat;
166  
167 0 ▶sub prepend {
Subroutine prepend: not called CC=1 SCAR=6.9
Pod documented
168 0 my ($self, $array) = @_;
169 0 $self->unshift(@$array);
170 0 $self;
171 }
172  
173 0 ▶sub _append_undestructive {
Subroutine _append_undestructive: not called CC=1 SCAR=6.9
174 0 my ($self, $array) = @_;
175 0 $self->dup->push(@$array);
176 }
177  
178 0 ▶sub _prepend_undestructive {
Subroutine _prepend_undestructive: not called CC=1 SCAR=6.9
179 0 my ($self, $array) = @_;
180 0 $self->dup->unshift(@$array);
181 }
182  
183 0 ▶sub add {
Subroutine add: not called CC=2 SCAR=17.9
Pod undocumented
184 0 my ($self, $array, $bool) = @_;
185 0 ▶ $bool ? $self->_prepend_undestructive($array)
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 0 0 0
186 : $self->_append_undestructive($array);
187 }
188  
189 0 ▶sub each {
Subroutine each: not called CC=3 SCAR=24.8
Pod documented
190 0 my ($self, $code) = @_;
191 0 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
192 0 $code->($_) for @{$self->dup};
193 0 $self;
194 }
195  
196 2 ▶sub collect {
Subroutine collect: called CC=3 SCAR=11.0
Pod documented
197 2 my ($self, $code) = @_;
198 2 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 2 2
199 2 my @collected = CORE::map &$code, @{$self->dup};
200 2 ▶ wantarray ? @collected : $self->new(\@collected);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
201 }
202  
203 12 *map = \&collect;
204  
205 1 ▶sub grep {
Subroutine grep: called CC=7 SCAR=22.5
Pod documented
206 1 my ($self, $code) = @_;
207 1 ▶ $code or return;
Branch: 1 branch
BranchTrueFalseTotal
$code or return 1 0 1
208 1 my @grepped;
209 1 ▶ if (!ref($code)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $code) { } 1 0 1
elsif (ref $code eq 'CODE') { } 0 0 0
210 1 for (@$self) {
211 4 ▶ CORE::push @grepped, $_ if $_->$code;
Branch: 1 branch
BranchTrueFalseTotal
if $_->$code 2 2 4
212 }
213 } elsif (ref $code eq 'CODE') {
214 0 @grepped = CORE::grep &$code, @$self;
215 } else {
216 0 croak "Invalid code";
217 }
218 1 ▶ wantarray ? @grepped : $self->new(\@grepped);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
219 }
220  
221 0 ▶sub find {
Subroutine find: not called CC=4 SCAR=30.0
Pod documented
222 0 my ($self, $code) = @_;
223 0 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
224 0 ▶ for (@$self) { &$code and return $_ }
Branch: 1 branch
BranchTrueFalseTotal
&$code and return $_ 0 0 0
225 }
226  
227 6 ▶sub index_of {
Subroutine index_of: called CC=4 SCAR=13.9
Pod documented
228 6 my ($self, $target) = @_;
229 6 ▶ my $code = (ref $target eq 'CODE') ? $target : sub { CORE::shift eq $target };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'CODE' ? : 1 5 6
230 6 ▶ for (my $i = 0; $i < $self->length; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->length) 17 1 18
231 17 ▶ &$code($self->[$i]) and return $i;
Branch: 1 branch
BranchTrueFalseTotal
&$code($self->[$i]) and return $i 5 12 17
232 }
233 }
234  
235 0 ▶sub sort {
Subroutine sort: not called CC=3 SCAR=24.8
Pod documented
236 0 my ($self, $code) = @_;
237 0 ▶ my @sorted = $code ? CORE::sort { $code->($a, $b) } @$self : CORE::sort @$self;
Branch: 1 branch
BranchTrueFalseTotal
$code ? : 0 0 0
238 0 ▶ wantarray ? @sorted : $self->new(\@sorted);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
239 }
240  
241 0 ▶sub compact {
Subroutine compact: not called CC=1 SCAR=6.9
Pod documented
242 0 ▶ CORE::shift->grep(sub { defined });
Subroutine __ANON__: not called CC=1 SCAR=6.9
243 }
244  
245 40 ▶sub length {
Subroutine length: called CC=1 SCAR=0.0
Pod documented
246 40 scalar @{$_[0]};
247 }
248  
249 12 *size = \&length;
250  
251 0 ▶sub flatten {
Subroutine flatten: not called CC=1 SCAR=6.9
Pod documented
252 0 my $self = CORE::shift;
253 0 ▶ $self->collect(sub { _flatten($_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
254 }
255  
256 0 ▶sub _flatten {
Subroutine _flatten: not called CC=3 SCAR=24.8
257 0 my $element = CORE::shift;
258 (ref $element and ref $element eq 'ARRAY')
259 0 ▶ ? CORE::map { _flatten($_) } @$element
Branch: 1 branch
BranchTrueFalseTotal
ref $element && ref $element eq 'ARRAY' ? : 0 0 0
Condition0%0/3
ref $element and ref $element eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $element and ref $element eq "ARRAY"
ref $element ref $element eq "ARRAY"
Truth table0%0/3
A and B
A= ref $elementB= ref $element eq "ARRAY"
ABresult
0X0
100
111
260 : $element;
261 }
262  
263 0 ▶sub is_empty {
Subroutine is_empty: not called CC=1 SCAR=6.9
Pod documented
264 0 !$_[0]->length;
265 }
266  
267 0 ▶sub uniq {
Subroutine uniq: not called CC=1 SCAR=6.9
Pod documented
268 0 my $self = CORE::shift;
269 0 $self->new([ List::MoreUtils::uniq(@$self) ]);
270 }
271  
272 0 ▶sub reduce {
Subroutine reduce: not called CC=2 SCAR=17.9
Pod documented
273 0 my ($self, $code) = @_;
274 0 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
275 0 ▶ List::Util::reduce { $code->($a, $b) } @$self;
Subroutine __ANON__: not called CC=1 SCAR=6.9
276 }
277  
278 2 ▶sub to_a {
Subroutine to_a: called CC=1 SCAR=0.0
Pod undocumented
279 2 my @unblessed = @{$_[0]};
280 2 \@unblessed;
281 }
282  
283 0 ▶sub as_list { # for Template::Iterator
Subroutine as_list: not called CC=1 SCAR=6.9
Pod undocumented
284 0 CORE::shift;
285 }
286  
287 2 ▶sub dup {
Subroutine dup: called CC=1 SCAR=0.0
Pod documented
288 2 __PACKAGE__->new($_[0]->to_a);
289 }
290  
291 0 ▶sub reverse {
Subroutine reverse: not called CC=1 SCAR=6.9
Pod documented
292 0 my $self = CORE::shift;
293 0 $self->new([ reverse @$self ]);
294 }
295  
296 0 ▶sub sum {
Subroutine sum: not called CC=1 SCAR=6.9
Pod documented
297 0 List::Util::sum @{$_[0]};
298 }
299  
300 12 1;
301  
302 =head1 NAME
303  
304 DBIx::MoCo::List - Array iterator for DBIx::MoCo.
305  
306 =head1 SYNOPSIS
307  
308 my $array_ref = [
309 {name => 'jkondo'},
310 {name => 'cinnamon'}
311 ];
312 my $list = DBIx::MoCo::List->new($array_ref);
313  
314 $list->size; #=> 2
315 my $first = $list->shift; #=> {name => 'jkondo'}
316 $list->push($first); #=> [{name => 'cinnamon'}, {name => 'jkondo'}];
317  
318 # DBIx::MoCo::List provides much more useful methods. For more
319 # details, see the sections below.
320  
321 =head1 METHODS
322  
323 =over 4
324  
325 =item dump ()
326  
327 Dump the content of C<$self> using L<Data::Dumper>.
328  
329 =item push ( I<@array> )
330  
331 =item unshift ( I<@array> )
332  
333 Sets the argument into C<$self>, a refernce to an array blessed by
334 DBIx::MoCo::List, like the same name functions provided by Perl core,
335 then returns a DBIx::MoCo::List object.
336  
337 my $list = DBIx::MoCo::List->new([qw(1 2 3)]);
338 $list->push(4, 5); #=> [1, 2, 3, 4, 5]
339 $list->unshift(0); #=> [0, 1, 2, 3, 4, 5]
340  
341 =item concat ( I<\@array> )
342  
343 =item prepend ( I<\@array> )
344  
345 They're almost the same as C<push()>/C<unshift()> described above
346 except that the argument shoud be a reference to an array.
347  
348 my $list = DBIx::MoCo::List->new([1, 2, 3]);
349 $list->concat([4, 5]); #=> [1, 2, 3, 4, 5]
350 $list->prepend([0]); #=> [0, 1, 2, 3, 4, 5]
351  
352 =item shift ()
353  
354 =item pop ()
355  
356 Pulls out the first/last element from C<$self>, a refernce to an array
357 blessed by DBIx::MoCo::List, then returns it like the same name
358 functions in Perl core.
359  
360 $list = DBIx::MoCo::List->new([1, 2, 3]);
361 $list->shift; #=> 1
362 $list->pop; #=> 3
363 $list->dump #=> [2]
364  
365 =item first ()
366  
367 =item last ()
368  
369 Returns the first/last element of C<$self>, a refernce to an array
370 blessed by DBIx::MoCo::List. These methods aren't destructive contrary
371 to C<shift()>/C<pop()> method.
372  
373 $list = DBIx::MoCo::List->new([1, 2, 3]);
374 $list->first; #=> 1
375 $list->last; #=> 3
376 $list->dump #=> [1, 2, 3]
377  
378 =item slice ( I<$start>, I<$end> )
379  
380 Returns the elements whose indexes are between C<$start> and C<$end>
381 as a DBIx::MoCo::List object.
382  
383 $list = DBIx::MoCo::List->new([qw(1 2 3 4)]);
384 $list->slice(1, 2) #=> [2, 3]
385  
386 =item zip ( I<\@array1>, I<\@array2>, ... )
387  
388 Bundles up the elements in each arguments into an array or a
389 DBIx::MoCo::List object along with the context.
390  
391 my $list = DBIx::MoCo::List->new([1, 2, 3]);
392 $list->zip([4, 5, 6], [7, 8, 9]);
393 #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
394  
395 # When the numbers of each list are different...
396 $list = DBIx::MoCo::List->new([1, 2, 3]);
397 $list->zip([4, 5], [7, 8, 9]);
398 #=> [[1, 4, 7], [2, 5, 8], [3, undef, 9]]
399  
400 my $list = DBIx::MoCo::List->new([1, 2]);
401 $list->zip([4, 5], [7, 8, 9]);
402 #=> [[1, 4, 7], [2, 5, 8]]
403  
404 =item delete ( I<$value>, I<$code> )
405  
406 Deletes the same values as C<$value> in C<$self>, a refernce to an
407 array blessed by DBIx::MoCo::List, and returns the value if found. If
408 the value is not found in C<$self> and C<$code> is passed in, the code
409 is executed using the value as an argument to find the value to be
410 deleted.
411  
412 $list = DBIx::MoCo::List->new([1, 2, 3, 2, 1]);
413 $list->delete(2); #=> 2
414 $list->dump #=> [1, 3, 1]
415  
416 =item delete_at ( I<$pos> )
417  
418 Deletes the element at C<$pos> and returns it.
419  
420 $list = DBIx::MoCo::List->new([1, 2, 3, 2, 1]);
421 $list->delete_at(3); #=> 2
422 $list->dump #=> [1, 2, 3, 1]
423  
424 =item delete_if ( I<$code> )
425  
426 Deletes the elements if C<$code> returns false value with each element
427 as an argument.
428  
429 $list = DBIx::MoCo::List->new([1, 2, 3, 4]);
430 $list->delete_if(sub { ($_ % 2) == 0) });
431 $list->dump #=> [2, 4]
432  
433 =item inject ( I<$result>, I<$code> )
434  
435 Executes folding calculation using C<$code> through each element and
436 returns the result.
437  
438 $list = DBIx::MoCo::List->new([1, 2, 3, 4]);
439 $list->inject(0, sub { $_[0] + $_[1] }); #=> 10
440  
441 =item join ( I<$delimiter> )
442  
443 Joins all the elements by C<$delimiter>.
444  
445 $list = DBIx::MoCo::List->new([0 1 2 3]);
446 $list->join(', ') #=> '0, 1, 2, 3'
447  
448 =item each_index ( I<$code> )
449  
450 Executes C<$code> with each index of C<$self>, a refernce to an array
451 blessed by DBIx::MoCo::List.
452  
453 $list = DBIx::MoCo::List->new([1, 2, 3]);
454 $list->each_index(sub { do_something($_) });
455  
456 =item each ( I<$code> )
457  
458 Executes C<$code> with each value of C<$self>, a refernce to an array
459 blessed by DBIx::MoCo::List.
460  
461 $list = DBIx::MoCo::List->new([1, 2, 3]);
462 $list->each(sub { do_something($_) });
463  
464 =item collect ( I<$code> )
465  
466 Executes C<$code> with each element of C<$self>, a refernce to an
467 array blessed by DBIx::MoCo::List using CORE::map() and returns the
468 results as a list or DBIx::MoCo:List object along with the context.
469  
470 $list = DBIx::MoCo::List->new([1, 2, 3]);
471 $list->map(sub { $_ * 2 }); #=> [2, 4, 6]
472  
473 =item map ( I<$code> )
474  
475 An alias of C<collect()> method described above.
476  
477 =item grep ( I<$code> )
478  
479 Executes C<$code> with each element of C<$self>, a refernce to an
480 array blessed by DBIx::MoCo::List using CORE::grep() and returns the
481 results as a list or DBIx::MoCo:List object along with the context.
482  
483 $list = DBIx::MoCo::List->new([qw(1 2 3 4)]);
484 $list->grep(sub { ($_ % 2) == 0 }); #=> [2, 4]
485  
486 =item find ( I<$code> )
487  
488 Returns the first value found in C<$self>, a refernce to an array
489 blessed by DBIx::MoCo::List, as a result of C<$code>..
490  
491 $list = DBIx::MoCo::List->new([1, 2, 3, 4]);
492 $list->find(sub { ($_ % 2) == 0 }); #=> 2
493  
494 =item index_of ( I<$arg> )
495  
496 Returns index of given target or given code returns true.
497  
498 $list = DBIx::MoCo::List->new([qw(foo bar baz)]);
499 $list->index_of('bar'); #=> 1
500 $list->index_of(sub { shift eq 'bar' }); #=> 1
501  
502 =item sort ( I<$code> )
503  
504 Sorts out each element and returns the result as a list or
505 DBIx::MoCo:List object along with the context.
506  
507 $list = DBIx::MoCo::List->new([qw(3 2 4 1]);
508 $list->sort; #=> [1, 2, 3, 4]
509 $list->sort(sub { $_[1] <=> $_[0] }); #=> [4, 3, 2, 1]
510  
511 =item compact ()
512  
513 Eliminates undefined values in C<$self>, a refernce to an array
514 blessed by DBIx::MoCo::List.
515  
516 $list = DBIx::MoCo::List->new([1, 2, undef, 3, undef, 4]);
517 $list->compact; #=> [1, 2, 3, 4]
518  
519 =item length ()
520  
521 Returns the length of C<$self>, a refernce to an array blessed by
522 DBIx::MoCo::List.
523  
524 $list = DBIx::MoCo::List->new([qw(1 2 3 4)]);
525 $list->length; #=> 4
526  
527 =item size ()
528  
529 An alias of C<length()> method described above.
530  
531 =item flatten ()
532  
533 Returns a list or DBIx::MoCo::List object which is recursively
534 flattened out.
535  
536 $list = DBIx::MoCo::List->new([1, [2, 3, [4], 5]]);
537 $list->flattern; #=> [1, 2, 3, 4, 5]
538  
539 =item is_empty ()
540  
541 Returns true if C<$self>, a refernce to an array blessed by
542 DBIx::MoCo::List, is empty.
543  
544 =item uniq ()
545  
546 Uniquifies the elements in C<$self>, a refernce to an array blessed by
547 DBIx::MoCo::List, and returns the result.
548  
549 $list = DBIx::MoCo::List->new([1, 2, 2, 3, 3, 4])
550 $list->uniq; #=> [1, 2, 3, 4]
551  
552 =item reduce ( I<$code> )
553  
554 Reduces the list by C<$code>.
555  
556 # finds the maximum value
557 $list = DBIx::MoCo::List->new([4, 1, 3, 2])
558 $list->reduce(sub { $_[0] > $_[1] ? $_[0] : $_[1] }); #=> 4
559  
560 See L<List::Util> to get to know about details of C<reduce()>.
561  
562 =item reverse ()
563  
564 Returns an reversely ordered C<$self>, a refernce to an array blessed
565 by DBIx::MoCo::List.
566  
567 $list = DBIx::MoCo::List->new([4, 1, 3, 2])
568 $list->reverse; #=> [2, 3, 1, 4]
569  
570 =item dup ()
571  
572 Returns a duplicated C<$self>, a refernce to an array blessed by
573 DBIx::MoCo::List.
574  
575 =item sum ()
576  
577 Returns the sum of each element in C<$self>, a refernce to an array
578 blessed by DBIx::MoCo::List.
579  
580 $list = DBIx::MoCo::List->new([1, 2, 3, 4]);
581 $list->sum; #=> 10
582  
583 =back
584  
585 =head1 AUTO-GENERATED METHODS
586  
587 =over 4
588  
589 =item map_XXX ()
590  
591 Returns the results of C<XXX()> method of each elements as a list or a
592 DBIx::MoCo::List object along with the context.
593  
594 my $list = Your::MoCo::Class->retrieve_all;
595 my $names = $list->map_name;
596  
597 In this case, C<$names> is a list of the return values of C<name()>
598 method of each element in C<$list>.
599  
600 =back
601  
602 =head1 SEE ALSO
603  
604 L<DBIx::MoCo>, L<List::Util>, L<List::MoreUtils>
605  
606 =head1 AUTHOR
607  
608 Junya Kondo, E<lt>jkondo@hatena.comE<gt>,
609 Naoya Ito, E<lt>naoya@hatena.ne.jpE<gt>,
610 Kentaro Kuribayashi, E<lt>kentarok@gmail.comE<gt>
611  
612 =head1 COPYRIGHT AND LICENSE
613  
614 Copyright (C) Hatena Inc. All Rights Reserved.
615  
616 This library is free software; you may redistribute it and/or modify
617 it under the same terms as Perl itself.
618  
619 =cut
« MoCo/Column/utf8.pm ↑ Summary MoCo/DataBase.pm »