Flex/Take.pm

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