blib/lib/Iterator/Flex/Take.pm

filter: Statementstmt 95.2% 100 / 105 Branchbran 66.6% 20 / 30 Conditioncond 53.8% 14 / 26 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 95.4% 21 / 22 Podpod 50.0% 1 / 2 totaltotal 80.9% 161 / 199 CC 27 SCAR 17.4
CC
27
Cov
83%
__ANON__
11.6
new
9.9
construct
7.3
CRAP 5.7
« blib/lib/Iterator/Flex/Product.pm ↑ Summary blib/lib/Iterator/Flex/Freeze.pm »
1 package Iterator::Flex::Take;
2  
3 # ABSTRACT: Take Iterator Class
4  
5 3 ▶use v5.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '0.33';
10  
11 3 ▶use Iterator::Flex::Utils ':IterAttrs', ':IterStates', ':SignalParameters', ':ExhaustionActions';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Iterator::Flex::Factory 'to_iterator';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Ref::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use parent 'Iterator::Flex::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67 13 ▶sub new ( $class, $iterable, $n, $pars = {} ) {
Subroutine new: called CC=7 SCAR=22.9
Pod documented
68  
69 13 my %pars = $pars->%*;
70  
71 13 ▶ defined $n && Scalar::Util::looks_like_number( $n ) && int( $n ) == $n && $n >= 0
Branch: 1 branch
BranchTrueFalseTotal
defined $n and &Scalar::Util::looks_like_number($n) and int $n == $n and $n >= 0 or throw_failure("parameter", "parameter \"n\" is not a positive integer") 0 13 13
Condition33%3/9
defined $n and &Scalar::Util::looks_like_number($n) and int $n == $n and $n >= 0
!ll&&!rl&&r
0013
defined $n and &Scalar::Util::looks_like_number($n) and int $n == $n
!ll&&!rl&&r
0013
defined $n and &Scalar::Util::looks_like_number($n)
!ll&&!rl&&r
0013
MC/DC0%0/4
defined $n and &Scalar::Util::looks_like_number($n) and int $n == $n and $n >= 0
defined $n &Scalar::Util::looks_like_number($n) int $n == $n $n >= 0
Truth table20%1/5
A and B and C and D
A= defined $nB= &Scalar::Util::looks_like_number($n)C= int $n == $nD= $n >= 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
72 || throw_failure( parameter => 'parameter "n" is not a positive integer' );
73  
74 13 ▶ my $lazy = delete $pars{lazy} // !!1;
Condition100%2/2
delete $pars{"lazy"} //
l!l
67
MC/DC100%1/1
delete $pars{"lazy"} //
delete $pars{"lazy"}
Truth table100%2/2
A
A= delete $pars{"lazy"}
Aresult
11
00
75  
76 13 $class->SUPER::new( {
77 n => $n,
78 lazy => $lazy,
79 src => $iterable,
80 },
81 \%pars,
82 );
83 }
84  
85 ▶sub construct ( $class, $state ) {
Subroutine construct: called CC=7 SCAR=19.8
Pod undocumented
86  
87 13 ▶ throw_failure( parameter => q{'state' parameter must be a HASH reference} )
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_hashref($state) 0 13 13
88 unless Ref::Util::is_hashref( $state );
89  
90 my ( $src, $prev, $current, $next, $array, $n, $lazy )
91 13 = @{$state}{qw[ src prev current next array n lazy ]};
92  
93 13 $src
94 = to_iterator( $src, { ( +EXHAUSTION ) => RETURN } );
95  
96 13 my $len;
97 13 ▶ $len = $array->@* if defined $array;
Branch: 1 branch
BranchTrueFalseTotal
if defined $array 0 13 13
98 13 ▶ $current //= undef;
Condition50%1/2
$current //= undef
l!l
013
MC/DC0%0/1
$current //= undef
$current
Truth table50%1/2
A
A= $current
Aresult
11
00
99 13 ▶ $prev //= undef;
Condition50%1/2
$prev //= undef
l!l
013
MC/DC0%0/1
$prev //= undef
$prev
Truth table50%1/2
A
A= $prev
Aresult
11
00
100 13 ▶ $next //= 0;
Condition50%1/2
$next //= 0
l!l
013
MC/DC0%0/1
$next //= 0
$next
Truth table50%1/2
A
A= $next
Aresult
11
00
101 13 my $took = 0;
102  
103 13 my $self;
104 13 my $is_exhausted = $src->can( 'is_exhausted' );
105 13 my $iterator_state;
106  
107 my %params = (
108  
109 ( +_NAME ) => 'itake',
110  
111 ( +_SELF ) => \$self,
112  
113 ( +STATE ) => \$iterator_state,
114  
115 # this iterator only depends on $src if it hasn't drained it.
116 # currently _DEPENDS must be a list of iterators, not a
117 # coderef, so a dynamic dependency is no tpossible
118 ( +_DEPENDS ) => $src,
119 ( +FREEZE ) => sub {
120 return [
121 0 ▶ $class,
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 {
123 src => $src,
124 prev => $prev,
125 current => $current,
126 next => $next,
127 array => $array,
128 n => $n,
129 lazy => $lazy,
130 },
131 ];
132 },
133 13 );
134  
135 13 ▶ if ( $lazy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lazy) { } 7 6 13
136  
137 $params{ +RESET } = sub {
138 4 ▶ $prev = $current = undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
139 4 $next = 0;
140 4 $took = 0;
141 7 };
142  
143 $params{ +REWIND } = sub {
144 4 ▶ $next = 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
145 4 $took = 0;
146 7 };
147  
148  
149 $params{ +PREV } = sub {
150 20 ▶ return $prev;
Subroutine __ANON__: called CC=1 SCAR=0.0
151 7 };
152  
153 $params{ +CURRENT } = sub {
154 20 ▶ return $current;
Subroutine __ANON__: called CC=1 SCAR=0.0
155 7 };
156  
157 $params{ +NEXT } = sub {
158  
159 111 ▶ return $self->signal_exhaustion
Subroutine __ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if $iterator_state == 1 or $took == $n 16 95 111
Condition100%3/3
$iterator_state == 1 or $took == $n
l!l&&r!l&&!r
10695
MC/DC100%2/2
$iterator_state == 1 or $took == $n
$iterator_state == 1 $took == $n
Truth table100%3/3
A or B
A= $iterator_state == 1B= $took == $n
ABresult
1X1
011
000
160 if $iterator_state == IterState_EXHAUSTED
161 || $took == $n;
162  
163 95 my $value = $src->();
164  
165 95 ▶ return $self->signal_exhaustion
Branch: 1 branch
BranchTrueFalseTotal
if not $value and $src->$is_exhausted 5 90 95
Condition100%3/3
not $value and $src->$is_exhausted
!ll&&!rl&&r
75155
MC/DC100%2/2
not $value and $src->$is_exhausted
not $value $src->$is_exhausted
Truth table100%3/3
not(A) and B
A= not $valueB= $src->$is_exhausted
ABresult
0X0
100
111
166 if !$value && $src->$is_exhausted;
167  
168 90 $took++;
169  
170 90 $prev = $current;
171 90 $current = $value;
172 90 return $current;
173 7 };
174 }
175  
176 else {
177  
178 $params{ +RESET } = sub {
179 4 ▶ $array = $prev = $current = undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
180 4 $len = undef;
181 4 $next = 0;
182 6 };
183  
184 $params{ +REWIND } = sub {
185 4 ▶ $prev = $array->[$current] if defined $current;
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if defined $current 4 0 4
186 4 $array = $current = undef;
187 4 $len = undef;
188 4 $next = 0;
189 6 };
190  
191 $params{ +PREV } = sub {
192 20 ▶ return $prev;
Subroutine __ANON__: called CC=1 SCAR=0.0
193 6 };
194  
195 $params{ +CURRENT } = sub {
196 20 ▶ return defined $current ? $array->[$current] : undef;
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $current ? : 20 0 20
197 6 };
198  
199 $params{ +NEXT } = sub {
200  
201 112 ▶ return $self->signal_exhaustion
Subroutine __ANON__: called CC=9 SCAR=24.5
Branch: 1 branch
BranchTrueFalseTotal
if $iterator_state == 1 10 102 112
202 if $iterator_state == IterState_EXHAUSTED;
203  
204 102 ▶ if ( !defined $array ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $array) 14 88 102
205 eval {
206 14 $array = [];
207  
208 # preload $array
209 14 push( $array->@*, $src->next );
210  
211 # postfix until/while check conditional first,
212 # which would be a problem if $array->@* and
213 # n == 0, in which case it would never
214 # call $src->next
215 14 ▶ push( $array->@*, $src->next ) until $array->@* == $n;
Branch: 1 branch
BranchTrueFalseTotal
@$array == $n or do { push @$array, $src->next } 98 14 112
216 14 1;
217 14 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $array = []; push @$array, $src->next; push @$array, $src->next until @$array == $n; 1 } } or do { die $@ unless Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted"); unless (@$array) { $current = undef; return $self->signal_exhaustion; } } 0 14 14
218 0 ▶ die $@
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted") 0 0 0
Condition0%0/3
Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted")
!ll&&!rl&&r
000
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($@) and $@->isa("Iterator::Flex::Failure::Exhausted")
Ref::Util::XS::is_blessed_ref($@) $@->isa("Iterator::Flex::Failure::Exhausted")
Truth table0%0/3
A and B
A= Ref::Util::XS::is_blessed_ref($@)B= $@->isa("Iterator::Flex::Failure::Exhausted")
ABresult
0X0
100
111
219 unless Ref::Util::is_blessed_ref( $@ )
220 && $@->isa( 'Iterator::Flex::Failure::Exhausted' );
221  
222 0 ▶ if ( !$array->@* ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$array) 0 0 0
223 0 $current = undef;
224 0 return $self->signal_exhaustion;
225 }
226 };
227  
228 14 $current = 0;
229 14 $next = 1;
230 14 $len = $array->@*;
231 14 return $array->[$current];
232 }
233  
234 88 ▶ if ( $next == $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next == $len) 10 78 88
235 10 $prev = $array->[$current];
236 10 return $self->signal_exhaustion;
237 }
238  
239 78 $prev = $array->[$current];
240 78 $current = $next++;
241 78 return $array->[$current];
242 6 };
243  
244 }
245  
246 13 return \%params;
247  
248 }
249  
250  
251 3 __PACKAGE__->_add_roles( qw[
252 State::Closure
253 Next::ClosedSelf
254 Rewind::Closure
255 Reset::Closure
256 Prev::Closure
257 Current::Closure
258 Freeze
259 ] );
260  
261 3 1;
262  
263 #
264 # This file is part of Iterator-Flex
265 #
266 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
267 #
268 # This is free software, licensed under:
269 #
270 # The GNU General Public License, Version 3, June 2007
271 #
272  
273 __END__
« blib/lib/Iterator/Flex/Product.pm ↑ Summary blib/lib/Iterator/Flex/Freeze.pm »