| 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::CodeIterator - Iterator implementation backed by a generator function | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 VERSION | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | This document describes Attean::CodeIterator version 0.040 | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use v5.14; | ||||||||||||||||||||||||||||
| 15 | use Attean; | ||||||||||||||||||||||||||||
| 16 | my $iter = Attean::CodeIterator->new( | ||||||||||||||||||||||||||||
| 17 | generator => sub { | ||||||||||||||||||||||||||||
| 18 | state $value = 0; | ||||||||||||||||||||||||||||
| 19 | Attean::Literal->new(++$value) | ||||||||||||||||||||||||||||
| 20 | }, | ||||||||||||||||||||||||||||
| 21 | item_type => 'Attean::API::Term', | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | say $iter->next->value; # 1 | ||||||||||||||||||||||||||||
| 25 | say $iter->next->value; # 2 | ||||||||||||||||||||||||||||
| 26 | say $iter->next->value; # 3 | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | The Attean::CodeIterator class represents a typed iterator. | ||||||||||||||||||||||||||||
| 31 | It conforms to the L<Attean::API::Iterator> role. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | The Attean::CodeIterator constructor requires two named arguments: | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =over 4 | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =item generator | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | A code reference that when called will return either the iterator's next item, | ||||||||||||||||||||||||||||
| 40 | or undef upon reaching the end of iteration. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =item item_type | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | A L<Type::Tiny> object representing the type of the items | ||||||||||||||||||||||||||||
| 45 | that will be returned from the iterator. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =back | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 METHODS | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =over 4 | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 50 | package Attean::CodeIterator 0.040 { | |||||||||||||||||||||||||||
| 56 | 50 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 50 | ▶ | use Type::Tiny::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 50 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 50 | ▶ | use Types::Standard qw(CodeRef ArrayRef); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 50 | ▶ | use Role::Tiny (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 50 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 50 | with 'Attean::API::Iterator'; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 50 | has generator => (is => 'ro', isa => CodeRef, required => 1); | |||||||||||||||||||||||||||
| 66 | 510 | ▶ | has _buffer => (is => 'ro', isa => ArrayRef, init_arg => undef, default => sub { [] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =item C<< next >> | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Returns the iterator's next item, or undef upon reaching the end of iteration. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1686 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 1686 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | 1686 | my $buffer = $self->_buffer; | |||||||||||||||||||||||||||
| 77 | 1686 | ▶ | if (scalar(@$buffer)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 1 | return shift(@$buffer); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 1685 | my @items = $self->generator->(); | |||||||||||||||||||||||||||
| 81 | 1684 | my $item = shift(@items); | |||||||||||||||||||||||||||
| 82 | 1684 | ▶ | return unless defined($item); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 1224 | ▶ | if (scalar(@items)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 2 | push(@$buffer, @items); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 1224 | my $role = $self->item_type; | |||||||||||||||||||||||||||
| 87 | 1224 | ▶ | if (Role::Tiny->is_role($role)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1086 | ▶ | 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)
| |||||||||||||||||||||||||||||
| 89 | 2 | die "CodeIterator item is not a $role: $item"; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 1222 | return $item; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 50 | 1; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||||||||||