| 1 | 50 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 50 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | Attean::ListIterator - Iterator implementation backed by a list/array of values | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 VERSION | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | This document describes Attean::ListIterator version 0.040 | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use v5.14; | ||||||||||||||||||||||||||||
| 15 | use Attean; | ||||||||||||||||||||||||||||
| 16 | my @values = map { Attean::Literal->new($_) } (1,2,3); | ||||||||||||||||||||||||||||
| 17 | my $iter = Attean::ListIterator->new( | ||||||||||||||||||||||||||||
| 18 | values => \@values, | ||||||||||||||||||||||||||||
| 19 | item_type => 'Attean::API::Term', | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | say $iter->next->value; # 1 | ||||||||||||||||||||||||||||
| 23 | say $iter->next->value; # 2 | ||||||||||||||||||||||||||||
| 24 | say $iter->next->value; # 3 | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | The Attean::ListIterator class represents a typed iterator. | ||||||||||||||||||||||||||||
| 29 | It conforms to the L<Attean::API::RepeatableIterator|Attean::API::Iterator> role. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | The Attean::ListIterator constructor requires two named arguments: | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =over 4 | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =item values | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | An array reference containing the items to iterate over. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =item item_type | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | A string representing the type of the items that will be returned from the | ||||||||||||||||||||||||||||
| 42 | iterator. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =back | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 METHODS | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =over 4 | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 50 | package Attean::ListIterator 0.040 { | |||||||||||||||||||||||||||
| 53 | 50 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 50 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 50 | ▶ | use Type::Tiny::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 50 | ▶ | use Types::Standard qw(ArrayRef Int); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 50 | ▶ | use Attean::API::Iterator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 50 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 50 | has values => (is => 'ro', isa => ArrayRef, required => 1); | |||||||||||||||||||||||||||
| 61 | 50 | has current => (is => 'rw', isa => Int, init_arg => undef, default => 0); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1151 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | 1151 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | 1151 | my $role = $self->item_type; | |||||||||||||||||||||||||||
| 66 | 1151 | foreach my $item (@{ $self->values }) { | |||||||||||||||||||||||||||
| 67 | 3651 | ▶ | if (Role::Tiny->is_role($role)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 1158 | ▶ | die "ListIterator item <$item> is not a $role" unless (blessed($item) and $item->does($role)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($item) and $item->does($role)
MC/DC100%2/2
builtin::blessed($item) and $item->does($role)
builtin::blessed($item)
$item->does($role)
Truth table100%3/3
A and B
A= builtin::blessed($item)B= $item->does($role)
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =item C<< reset >> | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Resets the iterator's internal state so that iteration begins again at the | ||||||||||||||||||||||||||||
| 76 | beginning of the values array. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 845 | ▶ | sub reset { | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 845 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | 845 | $self->current(0); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =item C<< next >> | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Returns the iterator's next item, or undef upon reaching the end of iteration. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 4785 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 4785 | my $self = shift; | |||||||||||||||||||||||||||
| 93 | 4785 | my $list = $self->values; | |||||||||||||||||||||||||||
| 94 | 4785 | my $index = $self->current; | |||||||||||||||||||||||||||
| 95 | 4785 | my $item = $list->[$index]; | |||||||||||||||||||||||||||
| 96 | 4785 | ▶ | return unless defined($item); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 3645 | $self->current(1+$index); | |||||||||||||||||||||||||||
| 98 | 3645 | return $item; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =item C<< size >> | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | Returns the number of elements still remaining in the iterator until it is | ||||||||||||||||||||||||||||
| 104 | fully consumed or until C<< reset >> is called. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 19 | ▶ | sub size { | ||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | 19 | return scalar(@{ $self->values }) - $self->current; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 50 | with 'Attean::API::RepeatableIterator'; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 50 | 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||