| 1 | package Catmandu::ArrayIterator; | ||
| 2 | |||
| 3 | 47 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 47 | our $VERSION = '1.0201_02'; | |
| 6 | |||
| 7 | 47 | ▶ | use Catmandu::Util qw(check_array_ref); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 47 | ▶ | use Role::Tiny::With; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 47 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 47 | with 'Catmandu::Iterable'; | |
| 12 | |||
| 13 | sub new { | ||
| 14 | 22 | ▶ | bless check_array_ref($_[1]), $_[0]; |
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||
| 15 | } | ||
| 16 | |||
| 17 | sub generator { | ||
| 18 | 11 | ▶ | my ($self) = @_; |
|
Subroutine
generator: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 19 | 11 | my $i = 0; | |
| 20 | sub { | ||
| 21 | 25 | ▶ | $self->[$i++]; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 22 | 11 | }; | |
| 23 | } | ||
| 24 | |||
| 25 | sub to_array { | ||
| 26 | 17 | ▶ | [@{$_[0]}]; |
|
Subroutine
to_array: called CC=1 SCAR=0.0
Pod
documented
| |||
| 27 | } | ||
| 28 | |||
| 29 | sub count { | ||
| 30 | 2 | ▶ | scalar @{$_[0]}; |
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||
| 31 | } | ||
| 32 | |||
| 33 | sub each { | ||
| 34 | 1 | ▶ | my ($self, $cb) = @_; |
|
Subroutine
each: called CC=2 SCAR=6.9
Pod
documented
| |||
| 35 | 1 | $cb->($_) for @$self; | |
| 36 | 1 | $self->count; | |
| 37 | } | ||
| 38 | |||
| 39 | sub first { | ||
| 40 | 1 | ▶ | $_[0]->[0]; |
|
Subroutine
first: called CC=1 SCAR=0.0
Pod
documented
| |||
| 41 | } | ||
| 42 | |||
| 43 | 47 | 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 | ||