line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::ArrayIterator; |
2
|
|
|
|
|
|
|
|
3
|
65
|
|
|
65
|
|
431059
|
use Catmandu::Sane; |
|
65
|
|
|
|
|
164
|
|
|
65
|
|
|
|
|
461
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
65
|
|
|
65
|
|
532
|
use Catmandu::Util qw(check_array_ref); |
|
65
|
|
|
|
|
181
|
|
|
65
|
|
|
|
|
3261
|
|
8
|
65
|
|
|
65
|
|
8686
|
use Role::Tiny::With; |
|
65
|
|
|
|
|
5485
|
|
|
65
|
|
|
|
|
3234
|
|
9
|
65
|
|
|
65
|
|
484
|
use namespace::clean; |
|
65
|
|
|
|
|
237
|
|
|
65
|
|
|
|
|
489
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Catmandu::Iterable'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
23
|
|
|
23
|
1
|
3082
|
bless check_array_ref($_[1]), $_[0]; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub generator { |
18
|
12
|
|
|
12
|
0
|
26
|
my ($self) = @_; |
19
|
12
|
|
|
|
|
22
|
my $i = 0; |
20
|
|
|
|
|
|
|
sub { |
21
|
28
|
|
|
28
|
|
387
|
$self->[$i++]; |
22
|
12
|
|
|
|
|
66
|
}; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub to_array { |
26
|
17
|
|
|
17
|
1
|
135
|
[@{$_[0]}]; |
|
17
|
|
|
|
|
69
|
|
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub count { |
30
|
2
|
|
|
2
|
1
|
1962
|
scalar @{$_[0]}; |
|
2
|
|
|
|
|
9
|
|
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub each { |
34
|
1
|
|
|
1
|
1
|
3
|
my ($self, $cb) = @_; |
35
|
1
|
|
|
|
|
5
|
$cb->($_) for @$self; |
36
|
1
|
|
|
|
|
1990
|
$self->count; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub first { |
40
|
1
|
|
|
1
|
1
|
6
|
$_[0]->[0]; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
1; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
__END__ |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=pod |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 NAME |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Catmandu::ArrayIterator - Convert an arrayref to an Iterable object |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 SYNOPSIS |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
use Catmandu::ArrayIterator; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $it = Catmandu::ArrayIterator->new([{n => 1}, {n => 2}, {n => 3}]); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
$it->each( sub { |
60
|
|
|
|
|
|
|
my $item = $_[0]; |
61
|
|
|
|
|
|
|
# Very complicated routine |
62
|
|
|
|
|
|
|
... |
63
|
|
|
|
|
|
|
}); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$it->[0]; |
66
|
|
|
|
|
|
|
# => {n => 1} |
67
|
|
|
|
|
|
|
$it->first; |
68
|
|
|
|
|
|
|
# => {n => 1} |
69
|
|
|
|
|
|
|
$it->map(sub { $_[0]->{n} + 1 })->to_array; |
70
|
|
|
|
|
|
|
# => [2, 3, 4] |
71
|
|
|
|
|
|
|
$it->count |
72
|
|
|
|
|
|
|
# => 3 |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 METHODS |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 new($arrayRef) |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Create a new iterator object from $arrayRef. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 to_array |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Return all the items in the Iterator as an ARRAY ref. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 each(\&callback) |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
For each item in the Iterator execute the callback function with the item as first argument. Returns |
87
|
|
|
|
|
|
|
the number of items in the Iterator. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 count |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Return the count of all the items in the Iterator. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 first |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Return the first item from the Iterator. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 SEE ALSO |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
L<Catmandu::Iterable>, L<Catmandu::Iterator> |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |