| 1 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | |||||||||||||||
| 4 | package Functional::Iterator; | ||||||||||||||
| 5 | 2 | ▶ | use base qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | our @EXPORT = qw(iterator); | |||||||||||||
| 8 | |||||||||||||||
| 9 | 2 | our $VERSION = 1.05; | |||||||||||||
| 10 | |||||||||||||||
| 11 | 17 | ▶ | sub iterator { __PACKAGE__->new(@_) } | ||||||||||||
|
Subroutine
iterator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 17 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 14 | 17 | my ($class, %args) = @_; | |||||||||||||
| 15 | 17 | return bless +{ | |||||||||||||
| 16 | %args, | ||||||||||||||
| 17 | index => 0, | ||||||||||||||
| 18 | }, $class; | ||||||||||||||
| 19 | } | ||||||||||||||
| 20 | |||||||||||||||
| 21 | 249 | ▶ | sub next { | ||||||||||||
|
Subroutine
next: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||
| 22 | 249 | my ($self) = @_; | |||||||||||||
| 23 | |||||||||||||||
| 24 | 249 | my $record; | |||||||||||||
| 25 | 249 | my $index = $self->{index}; | |||||||||||||
| 26 | |||||||||||||||
| 27 | 249 | ▶ | if (exists $self->{generated_record}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 28 | # If the previous call to ->next() yielded an iterator, then we stashed that | ||||||||||||||
| 29 | # iterator into $self->{generated_record} and returned that stashed iterator's | ||||||||||||||
| 30 | # ->next record. Now it's time to ask that stashed previously-generated-but | ||||||||||||||
| 31 | # now-active iterator for another record. | ||||||||||||||
| 32 | 40 | $record = delete $self->{generated_record}; | |||||||||||||
| 33 | } elsif (exists $self->{generator}) { | ||||||||||||||
| 34 | # We have no current stashed iterator: either our generator doesn't produce | ||||||||||||||
| 35 | # iterators, or the last iterator it produced has been exhausted. | ||||||||||||||
| 36 | 20 | $record = $self->{generator}->(); | |||||||||||||
| 37 | } else { | ||||||||||||||
| 38 | # Oh, we're just a simple iterator over records, how nice. | ||||||||||||||
| 39 | 189 | $record = $self->{records}[$index]; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 249 | ▶ | if (UNIVERSAL::isa($record, ref($self))) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | $self->{generated_record} = $record # The putative $record is actually an iterator, so stash it away... | ||||||||||||||
| 44 | 101 | ▶ | if exists $self->{generator}; # ...(maybe)... | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 101 | $record = $record->next; # ...and ask it for its next record. | |||||||||||||
| 46 | |||||||||||||||
| 47 | 101 | ▶ | if (! defined($record)) { # Whoops, the stashed-away iterator is exhausted... | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | 14 | ▶ | if (exists $self->{records}[$index + 1]) { # Simple iterator-over-records case | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 5 | $self->{index}++; | |||||||||||||
| 50 | 5 | return $self->next; | |||||||||||||
| 51 | } else { # Oh, there's no next record available: why not? | ||||||||||||||
| 52 | 9 | ▶ | if (exists $self->{generator}) { # ...because we're an iterator over generated iterators. | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 5 | delete $self->{generated_record}; # ...Okay, discard this iterator (it's clearly exhausted) | |||||||||||||
| 54 | 5 | return $self->next; # ...and ask myself for another | |||||||||||||
| 55 | } else { # ...because we're an iterator over records | ||||||||||||||
| 56 | 4 | return undef; # ...but there is no next record, so signal exhaustion | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | } | ||||||||||||||
| 59 | } | ||||||||||||||
| 60 | } else { | ||||||||||||||
| 61 | 148 | $self->{index}++; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | 235 | ▶ | return undef unless defined $record; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | |||||||||||||||
| 65 | return $self->{mutator} | ||||||||||||||
| 66 | 217 | ▶ | ? $self->{mutator}->($record) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | : $record; | ||||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 3 | ▶ | sub reset { | ||||||||||||
|
Subroutine
reset: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 71 | 3 | my ($self) = @_; | |||||||||||||
| 72 | 3 | $self->{index} = 0; | |||||||||||||
| 73 | 3 | delete $self->{generated_record}; | |||||||||||||
| 74 | 3 | foreach (grep { UNIVERSAL::isa($_, __PACKAGE__) } @{$self->{records}}) { | |||||||||||||
| 75 | 2 | $_->reset; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 2 | 1; | |||||||||||||
| 80 | |||||||||||||||
| 81 | __END__ | ||||||||||||||